Search code examples
pythondiscorddiscord.pynextcord

Nextcord: How do I send a message to a specific channel in cogs?


I'm using python 3.10.2. I'm making an edit/delete logging function to my discord bot using Nextcord and then the bot would send it to my logs channel.

class Logs(commands.Cog):
    def __init__(bot, self):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message_delete(self, message):
        embed = nextcord.Embed(
            title=f"`{message.author.name}` deleted a message. | User id: <{message.author.id}>", description=f"{message.content}", color=0xffffff)
        channel = bot.get_channel(950093169515696181)
        await channel.send(embed=embed)

    @commands.Cog.listener()
    async def on_message_edit(self, message_before, message_after):
        embed = nextcord.Embed(
            title=f"`{message_before.author.name}` edited a message. | User id: {message_before.author.id}", color=0xffffff)
        embed.add_field(name="Before edit:",
                        value=f"{message_before.content}", inline=False)
        embed.add_field(name="After edit:",
                        value=f"{message_after.content}", inline=False)
        channel = bot.get_channel(950093169515696181)
        await channel.send(embed=embed)


def setup(bot):
    bot.add_cog(Logs(bot))

But when I'm trying to trigger edit or delete, I get the error saying:

  File "F:\bot\cogs\logger.py", line 20, in on_message_delete
    await channel.send(embed=embed)
AttributeError: 'NoneType' object has no attribute 'send'

If someone knows how to fix this I would appreciate it. I checked the official site too, but it is the same as my code, so... (https://nextcord.readthedocs.io/en/latest/faq.html#how-do-i-send-a-message-to-a-specific-channel)


Solution

  • It is self.bot.get_channel(id)