当前位置: 首页 > 知识库问答 >
问题:

Discord.py机器人踢/DM命令

缑智敏
2023-03-14

我正在学习如何使用python创建一个discord bot,但我在使用这个命令时遇到了问题。我试图做的是踢一个特定的用户,然后使用bot将邀请发送回discord服务器。这是一个愚蠢的想法,但我真的想让它发挥作用。

我特别遇到的问题是如何踢一个特定的用户(带有用户标识),然后将该用户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's john发出服务器邀请。

共有3个答案

张伯寅
2023-03-14
@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)")
左丘子平
2023-03-14

只需更换所有的

    @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>")
秦育
2023-03-14

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

@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 
 类似资料:
  • 下面是代码: 出于某种原因,即使我使用用户id,bot也总是输出“未找到用户”。机器人有权这么做,有人知道解决办法吗?

  • 我试图做一个不和谐机器人,它创建一个类别,并为角色和用户设置权限,将用户id作为参数: 其中args是包含命令、类别名称和用户ID的字符串数组。用法应为: 我对此有一些问题,因为就像机器人看不到服务器的成员,他唯一可以添加的用户是我,如果在第一个参数中指定服务器的所有者(在我的例子中是000000)。如果我把别人的ID,机器人不会在类别中添加该用户的权限。我发现,也许机器人看不到其他用户,事实上,

  • 我是一个初学者程序员(或者在我看来是这样),我需要帮助在音乐机器人中实现一个队列。 目前,只有当队列中有一首歌时,队列才能正常工作。如果有更多的歌曲,那么递归开始(在=wait serverQueue(语音,消息)之后从def play开始并等待play(queue.pop(0),语音,消息)从队列开始),并且所有歌曲只是跳过。 我试图以不同的方式解决这个问题,例如,引入了第二个变量,但这并没有带

  • 问题内容: 我正在使用Python处理User Discord Bot。如果机器人所有者键入内容,则该机器人将DM所有者提到的用户。 问题答案: 最简单的方法是使用扩展程序。在这里,我们使用转换器来获取目标用户,并使用仅关键字参数作为可选消息来发送目标用户: 对于discord.py的较新的1.0+版本,应使用而不是

  • 我目前正在制作一个discord bot,并希望在服务器加入时在服务器的通道中发送一条消息,这是我到目前为止的代码。 当我运行这段代码时,什么都不会发生。我没有得到任何ERORR或输出。 如果有人能帮忙,那就太棒了。谢谢

  • 在我的不和谐中,我有几个角色,比如“所有者”、“成员”和“监狱”。我希望bot只能由“所有者”角色访问,并希望命令如下所示:。监狱@user。然后机器人应该去掉“成员”角色,给他们“监狱”角色。 不和谐服务器最新更新请在此输入图像描述