Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to send a message to a specific channel using Discord.py rewrite and it isn't working

I'm currently working on a discord bot and I'm trying to send a message to a specific channel using Discord.py rewrite once the user levels up, and I'm getting this error:

   await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
AttributeError: 'NoneType' object has no attribute 'message'

Here is all the code:

import discord
from discord.ext import commands

import json
import asyncio

class Levels(commands.Cog):
    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author == self.bot.user:
            return

        author_id = str(message.author.id)
        bot = commands.Bot(command_prefix='!')

        if author_id not in self.users:
            self.users[author_id] = {}
            self.users[author_id]['level'] = 1
            self.users[author_id]['exp'] = 0

        self.users[author_id]['exp'] += 1

        if author_id in self.users:
            if self.lvl_up(author_id):
                channel = bot.get_channel('636399538650742795')
                await channel.message.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")

    def __init__(self, bot):
        self.bot = bot

        with open(r"cogs\userdata.json", 'r') as f:
            self.users = json.load(f)

            self.bot.loop.create_task(self.save_users())

    async def save_users(self):
        await self.bot.wait_until_ready()
        while not self.bot.is_closed():
            with open(r"cogs\userdata.json", 'w') as f:
                json.dump(self.users, f, indent=4)

            await asyncio.sleep(5)


    def lvl_up(self, author_id):
        author_id = str(author_id)
        current_xp = self.users[author_id]['exp']
        current_lvl = self.users[author_id]['level']
        if current_xp >= ((3 * (current_lvl ** 2)) / .5):
            self.users[author_id]['level'] += 1
            return True
        else:
            return False

I'm really not sure what the issue is here but if anyone knows the problem I would be very appreciative if you could let me know how I can correct this.

Thanks for reading I've been trying to figure this out for hours.

Edit: Still having the issue.

like image 858
ilavywavy Avatar asked Dec 09 '19 20:12

ilavywavy


People also ask

How to send a multi-letter message in discord?

The colon and discord.Channel signifies you need to enter an #channel format on discord screen Also to have a multi letter message, do this: So Hello, and, bye are parts of a tuple, like this: ('Hello','and','bye') so when you do it joins the elements of tuple to give you a string, separeted by spaces. It works for lists too. Hope this helps

What does the colon and Discord channel mean in a tuple?

The colon and discord.Channel signifies you need to enter an #channel format on discord screen Also to have a multi letter message, do this: So Hello, and, bye are parts of a tuple, like this: ('Hello','and','bye') so when you do

Where can I get a discord Message counter bot?

If there are any Java devs, here's the source code (JDA): https://github.com/JohnnnnyKlayy/DiscordMessageCounterBot Feel free to do whatever you want with it. Here's a demo that I'll host for a month: https://discord.com/api/oauth2/authorize?client_id=794676940287508510&permissions=2048&scope=bot


1 Answers

You get AttributeError because channel is None.

To fix it you need to remove quotes from channel id like this:

channel = bot.get_channel(636399538650742795)

This is described here: https://discordpy.readthedocs.io/en/latest/migrating.html#snowflakes-are-int


Also i see another error on the next line. The channel has no message attribute too. I think you need to fix it like this:

await channel.send(f"{message.author.mention} is now level {self.users[author_id]['level']}! congrats!")
like image 98
Sergree Avatar answered Sep 23 '22 17:09

Sergree