简体   繁体   English

如何使用斜杠命令踢用户 Discord.py

[英]How to kick a user using slash commands Discord.py

I'm trying to make my Discord bot kick a member, and send that "user banned because reason" to a specific channel and not the channel the command was used.我试图让我的 Discord 机器人踢一个成员,并将“用户因原因被禁止”发送到特定频道,而不是使用命令的频道。 The code I'm using:我正在使用的代码:

@bot.slash_command(description = "Kick someone", guild_ids=[1041057700823449682])
@commands.has_permissions(kick_members=True)
@option("member",description = "Select member")
@option("reason",description = "Reason for kick (you can leave this empty)")
async def kick(
    ctx, 
    member: discord.Member,
    channel: bot.get_channel(1042042492020863037),
    *, 
    reason=None):
    if reason==None:
      reason="(no reason)"
    await ctx.guild.kick(member)
    await ctx.respond("Done :)")
    await ctx.channel.send(f'User {member.mention} was kicked because {reason}')

When I try using this code I get a few errors:当我尝试使用此代码时,出现了一些错误:

Traceback (most recent call last):
  File "c:\Users\fonti\Documents\Projetos Python\Bot do Discord\Iniciar Bot.py", line 152, in <module>
    async def kick(
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 905, in decorator
    self.add_application_command(result)
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\bot.py", line 127, in add_application_command
    command._set_cog(None)
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 603, in _set_cog
    self.cog = cog
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 827, in cog
    self._validate_parameters()
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 705, in _validate_parameters
    self.options: list[Option] = self._parse_options(params)
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\core.py", line 745, in _parse_options
    option = Option(option)
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\commands\options.py", line 210, in __init__
    self.input_type = SlashCommandOptionType.from_datatype(input_type)
  File "C:\Users\fonti\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\enums.py", line 707, in from_datatype
    if datatype.__name__ in ["Member", "User"]:
AttributeError: 'NoneType' object has no attribute '__name__'. Did you mean: '__ne__'?

I was trying to send the message...我试图发送消息...

(f'User {member.mention} was kicked because {reason}')

to a specific channel.到特定频道。 If I remove the channel condition, the bot works, but sends this message to the channel the command was used.如果我删除通道条件,机器人会工作,但会将此消息发送到使用命令的通道。

I believe the cause of your error is your channel definition inside your kick command definition.我相信错误的原因是您的 kick 命令定义中的通道定义。 Try removing the channel definition from your kick command definition and put it inside the function instead.尝试从踢命令定义中删除通道定义,并将其放入 function 中。 The way I have it setup on my bot, other than the channel definition, is the same as yours and mine works perfectly除了频道定义外,我在我的机器人上设置它的方式与你和我的一样完美

To send it in the channel, instead of using ctx.channel.send , you can use ctx.send .要在频道中发送它,而不是使用ctx.channel.send ,您可以使用ctx.send I think that's where you're running into your error.我认为这就是您遇到错误的地方。 Also here's how I tend to set up my kick command using slash commands so that my answer makes more sense:这也是我倾向于使用斜线命令设置我的踢命令的方式,以便我的回答更有意义:

@nextcord.slash_command() # I use nextcord, a dpy fork so your setup is gonna be different
@commands.has_permissions(whatever permissions you want)
async def kickuser(self, ctx, member : nextcord.Member, *, reason='None'): # default reason is none so that it is optional in the slash command
      # side note for nextcord.Member, having it there makes it so that there's a drop down menu that functions the same way as if you were to @ someone in a message. This makes it easier to kick the right person        
      embed = discord.Embed(description=f'You have been kicked from {ctx.guild} for reason: {reason}')
      
      embed = nextcord.Embed(description=f'{member} has been kicked for reason: {reason}') # setting up embed to send
      await ctx.send(embed=embed) # sends the embed in chat letting people know the person has been kicked
      await member.kick(reason=reason) # actually kicking the person, this comes after all the other steps so that we are able to mention and direct message them that they have been kicked

Hope this helps希望这可以帮助

This snip of code uses PyCord ( Confirmed to work by myself )这段代码使用了 PyCord(我自己确认可以工作)

@discord.default_permissions(kick_members = True)
async def kick(ctx, member : discord.Member, *, reason=None):
        await member.kick(reason=reason)
        await ctx.respond(f'{member.mention} has been kicked!')

Just get a channel object with bot.get_channel()只需使用bot.get_channel()获取频道 object

Then use the channels send() function to send your message.然后使用通道send() function 发送您的消息。 Also, : after arguments in function definitions are type hints, they are made for IDEs, if you want to assign a default value, you have to use = instead.另外, :在 function 定义中的 arguments 之后是类型提示,它们是为 IDE 制作的,如果要分配默认值,则必须使用=代替。 (Look at your channel argument) (看看你的频道参数)

EDIT编辑

You are using type hints in your code.您正在代码中使用类型提示 Type hints are made for IDEs in first place, so they can show you mistakes in your code more easier.类型提示首先是为 IDE 设计的,因此它们可以更容易地向您显示代码中的错误。 But you are „setting“ a value in it with the function, but this is for discord.py None , thats causing your error.但是您正在使用 function 在其中“设置”一个值,但这是针对 discord.py None的值,这会导致您的错误。 Use : for showing which class an argument has to be.使用:显示参数必须是哪个 class。 But use = for setting a default value , if this argument is not being passed.但如果未传递此参数,请使用=设置默认值

def function(type_hint: str, default_value = 0, mixed : int = 10):
    print(type_hint, default_value, mixed)

Answer again if you need even further help;-)如果您需要进一步的帮助,请再次回答;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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