【问题标题】:Discord.py Bot Kick/DM CommandDiscord.py Bot Kick/DM 命令
【发布时间】:2019-08-22 05:02:31
【问题描述】:

我正在学习如何使用 python 创建一个不和谐的机器人,但我在使用这个命令时遇到了问题。我想要做的是踢一个特定的用户,然后使用机器人向他们发送邀请回到不和谐服务器。这是一个愚蠢的想法,但我真的很想让它发挥作用。

我特别遇到的问题是如何踢特定用户(使用用户 ID),然后 DM 该用户。

谢谢!

代码如下:

if message.content == '!kickjohn':
    if "527290609166188554" in [role.id for role in message.author.roles]:
        <KICK JOHN COMMAND>
        await client.send_message(message.channel, "_**Bye Bye John!**_")
        await client.send_message(<JOHN>, 'https://discord.gg/XXXXXXX')
    else:
        await client.send_message(message.channel, "sorry you can't do that")

这样做的目的是,如果具有适当角色类型的某人!kickjohn 特定的不和谐用户 ID (john) 被踢出,机器人会自动 dm 的 john 邀请到服务器。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    这是一款适合您的完美的作品

    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix="!", case_insensitive=True)
    token = "XXXXyyyyZzZz:AAAAAbbbbbCCCCCddddd"
    
    @bot.command()
    async def kick(ctx, member:discord.Member):
        await ctx.send(f"{member.mention} has been kicked from this server by {ctx.author.mention}")
        dm = await member.create_dm()
        server_name = "name of your server"
        invite_link = "put_link_here"
        await member.kick()
        await dm.send(f"You got kicked out of the server {server_name},\nYou can join again - {invite_link}")
    
    bot.run(token)
    

    现在你什么时候使用 - !kick @jhon 机器人会踢 jhon 并向他发送带有邀请链接的 dm....

    【讨论】:

      【解决方案2】:
      @client.command(description="kicks a user with specific reason (only admins)") #kick
      @commands.has_permissions(administrator=True)
      async def kick (ctx, member:discord.User=None, reason =None):
       try:
          if (reason == None):
              await ctx.channel.send("You  have to specify a reason!")
              return
          if (member == ctx.message.author or member == None):
              await ctx.send("""You cannot kick yourself!""") 
      
          message = f"You have been kicked from {ctx.guild.name} for {reason}"
          await member.send(message)
          await ctx.guild.kick(member, reason=reason)
          print(member)
          print(reason)
          await ctx.channel.send(f"{member} is kicked!")
       except:
          await ctx.send(f"Error kicking user {member} (cannot kick owner or bot)")
      

      【讨论】:

        【解决方案3】:

        只需将所有 替换为您想要的内容

            @client.command(pass_context=True)
            async def kick(ctx, user: discord.Member):
                if "527290609166188554" in [role.id for role in ctx.message.author.roles]:
                    await client.send_message(user, "<message><link>")
                    await client.kick(user)
                    await client.say("{} Just got Kicked".format(user.name))
                else:
                    await client.say("<access denied because of improper role message>")
        

        【讨论】:

          【解决方案4】:

          我认为你应该使用一个命令来使它更容易,如果你有一个on_message 函数添加await bot.process_commands(message) 像这样

          @bot.event
          async def on_message(message):
              await bot.process_commands(message)
          
          @commands.has_role("role_name_here")#makes it so that only works with specific role
          @bot.command(pass_context=True)
          async def kick(msg,user:discord.Member): #this converts the member you mention into a usuer object, you can also do it by user id or server nickname if you don't want to mention them
              """[Create an invite code then kicks the user]
              """
              code=await bot.create_invite(msg.message.channel) #create the invite code
              await bot.send_message(user,f'{code}') #Send the invite code first because you must have common server to send message to user
              await bot.kick(user) #kick the user 
          

          【讨论】:

          • 感谢您的回复!我已将其放入我的代码中,但我真的不知道如何使用它。我必须输入什么才能使其真正起作用?对不起大声笑
          • 假设bot前缀是.可以使用下面的方法.kick @Cheng Yue,.kick Cheng Yue或者用户ID.kick 19234819283
          猜你喜欢
          • 2018-03-04
          • 2021-05-17
          • 2021-03-01
          • 1970-01-01
          • 1970-01-01
          • 2020-11-13
          • 1970-01-01
          • 2022-01-05
          • 2021-01-05
          相关资源
          最近更新 更多