簡體   English   中英

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

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

我試圖讓我的 Discord 機器人踢一個成員,並將“用戶因原因被禁止”發送到特定頻道,而不是使用命令的頻道。 我正在使用的代碼:

@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}')

當我嘗試使用此代碼時,出現了一些錯誤:

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__'?

我試圖發送消息...

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

到特定頻道。 如果我刪除通道條件,機器人會工作,但會將此消息發送到使用命令的通道。

我相信錯誤的原因是您的 kick 命令定義中的通道定義。 嘗試從踢命令定義中刪除通道定義,並將其放入 function 中。 除了頻道定義外,我在我的機器人上設置它的方式與你和我的一樣完美

要在頻道中發送它,而不是使用ctx.channel.send ,您可以使用ctx.send 我認為這就是您遇到錯誤的地方。 這也是我傾向於使用斜線命令設置我的踢命令的方式,以便我的回答更有意義:

@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

希望這可以幫助

這段代碼使用了 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!')

只需使用bot.get_channel()獲取頻道 object

然后使用通道send() function 發送您的消息。 另外, :在 function 定義中的 arguments 之后是類型提示,它們是為 IDE 制作的,如果要分配默認值,則必須使用=代替。 (看看你的頻道參數)

編輯

您正在代碼中使用類型提示 類型提示首先是為 IDE 設計的,因此它們可以更容易地向您顯示代碼中的錯誤。 但是您正在使用 function 在其中“設置”一個值,但這是針對 discord.py None的值,這會導致您的錯誤。 使用:顯示參數必須是哪個 class。 但如果未傳遞此參數,請使用=設置默認值

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

如果您需要進一步的幫助,請再次回答;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM