Search code examples
discorddiscord.pybots

How to call functions with Discord.py?


Calling functions like normal gives me an error. :(

Every time I try to do await warn(ctx, member) it gives me an error AttributeError: 'str' object has no attribute 'name'

@bot.hybrid_command()
async def warn(ctx: commands.Context, member: discord.Member):
    await ctx.send(f"Please, {member.name} do not send any form of bad words, Thank you!", ephemeral=True)

@bot.event
async def on_message(message): #checking/reading the messages
    guild = bot.get_guild(SEVER_ID)
    channel2 = bot.get_channel(GENERAL_CHANNEL)
    

    if guild is None:
        print("No Guild")
        return
        
    channel = guild.get_channel(REPORT_CHANNEL)

    if message.author == bot.user:
        return

    result = profanity.contains_profanity(message.content)

    if result == True:   #deciding if they are bad and adding a report
        badword = True
        await channel.send("**Message: **" + message.content + " | " + "**User: **" + message.author.name)
        await channel.send("<@&1284787770874658939>")
        member = guild.get_member(message.author.id)
        await warn(ctx, member)
        await message.delete()

        await bot.process_commands(message)

it gives me an error with the "warn" command and "on_message" event

can anyone help?

anything would be appreciated!


Solution

  • When you use the @bot.hybrid_command() decorator, you're converting your warn() function into a command, which implies changes to the invocation parameters that I haven't looked into. But in any case, if you need to call your warn() function in other parts of the code, I recommend that you isolate it from the warn command. For example:

    async def warn(msg: discord.Message):
        await msg.reply(f"Please, {msg.author.name} do not send any form of bad words, Thank you!", delete_after=5)
    
    @bot.hybrid_command(name="warn")
    async def warn_cmd(ctx: commands.Context, message: discord.Message):
        await warn(message)
        await ctx.reply(f"Message {message.jump_url} warned.", ephemeral=True)
    
    @bot.event
    async def on_message(message: discord.Message): #checking/reading the messages
        guild = bot.get_guild(SEVER_ID)
        channel2 = bot.get_channel(GENERAL_CHANNEL)
        
    
        if guild is None:
            print("No Guild")
            return
            
        channel = guild.get_channel(REPORT_CHANNEL)
    
        if message.author == bot.user:
            return
    
        result = profanity.contains_profanity(message.content)
    
        if result == True:   #deciding if they are bad and adding a report
            badword = True
            await channel.send("**Message: **" + message.content + " | " + "**User: **" + message.author.name)
            await warn(message)
    
            await bot.process_commands(message)