简体   繁体   中英

I am making a warn function on my discord.py bot and I don't know why it is giving me an attribute error

@client.command()
@commands.has_permissions(kick_members = True)
async def warn(self,ctx,member = discord.Member,*,reason = None):

    await member.send("You were warned in {} for: {}".format(member.guild,reason))

    embed = discord.Embed(":poop:***{} has been warned".format(member))

    await ctx.send(embed=embed)
Traceback (most recent call last):
  File "C:\Users\yomamahahaha\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "c:/Users/yomamahahaha/Desktop/poopshitter/bot.py", line 96, in warn
    await member.send("You were warned in {} for: {}".format(member.guild,reason))
  File "C:\Users\yomamahahaha\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\abc.py", line 864, in send
    channel = await self._get_channel()
AttributeError: 'str' object has no attribute '_get_channel'

I don't have get_channel in this code so I was wondering what the problem would be.

I've noticed 2 things.

  1. You want to use : instead of = in line 3. If you use : discord.py will try to search for a user with that ID, Mention, etc.. = is used to pass optional arguments. That means you do not have to specify the reason when you run that command.

  2. The discord.Embed() function will not work like you've shown it here. The correct way to call this function is embed = discord.Embed(description= f":poop:***{member} has been warned") . That way discord.py knows how the embed should look like. Embeds have quite a few customizable things. Examples: Title, description, color, images and multiple fields.

Here is the command with my suggestions. If you have any questions about it, let me know.

@client.command()
@commands.has_permissions(kick_members = True)
async def warn(self, ctx, member: discord.Member, *, reason = "No reason provided"):
     await member.send(f"You were warned in {member.guild}. Reason: {reason}")
     embed = discord.Embed(description=f":poop:***{member} has been warned.")
     await ctx.send(embed=embed)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM