Discord.py添加角色命令

Discord.py添加角色命令,discord,discord.py,bots,Discord,Discord.py,Bots,我刚开始制造一个不和谐机器人。我想做一个命令,在那里你可以键入-queue duo,与其他想要排队的人一起排队。但是,我的addrole命令完全没有响应,在Discord中键入该命令时不会给出角色或错误 import discord import os #run the Bot and a message to make sure it ran client = discord.Client() @client.event async def on_ready(): print('

我刚开始制造一个不和谐机器人。我想做一个命令,在那里你可以键入-queue duo,与其他想要排队的人一起排队。但是,我的addrole命令完全没有响应,在Discord中键入该命令时不会给出角色或错误

import discord
import os


#run the Bot and a message to make sure it ran
client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return
#Detects if someone queues
    if message.content.startswith('-queue duos'):

        await message.channel.send('you have joined the duos queue.')

        async def role(ctx, member : discord.Member, role : discord.Role):
          await member.add_roles(role)
     

client.run(os.getenv('TOKEN'))

因为您已定义但未调用角色函数。
我想您看到了一个使用discord.ext.commands的教程,并尝试创建一个命令。
我建议您使用discord.ext.commands.Bot而不是discord.Client。
它是客户端的一个子类,因此您也可以这样做。
例如:

导入不一致
从discord.ext导入命令
bot=commands.bot(命令前缀='-')
botId=
@机器人事件
_ready()上的异步定义:
等待bot.change_状态(活动=discord.Game(“使用您的服务器”))
打印(“机器人准备就绪”)
@机器人事件
异步def on_消息(消息):
如果message.content==f“”或message.content==f“”:
等待message.channel.send(“我的前缀是-”)
其他:
等待bot.process_命令(消息)
@bot.command()
异步定义角色(ctx,成员:discord.member,角色:discord.role,*原因):
原因=“”。加入(原因)
尝试:
等待成员。添加角色(角色,原因=原因)
等待ctx.channel.send(“角色添加到成员”)
除:
等待ctx.channel.send(“我想我没有这样做的权限”)
bot.run(令牌)

正如我所见,您在一次活动中试图这样做。为此,您需要识别
消息的
作者
,然后为其分配角色。您在代码中犯了一些错误,因为另一个函数不属于某个事件

查看以下代码:

从discord.utils导入获取
@客户端事件
异步def on_消息(消息):
如果message.author==client.user:
返回
#检测是否有人排队
如果message.content.startswith('-queue duos'):
role=discord.utils.get(message.guild.roles,id=RoleID)#定义角色
等待消息。作者。添加角色(角色)#将角色添加到作者
wait message.channel.send('您已加入duos队列')
我们做了什么?

  • 通过
    discord.utils.get
  • 确定了邮件的作者并分配了角色

也许还可以再看一次

您的问题是,它没有注册为命令。您应该使用role命令的特殊条件来执行此操作,并在外部注册它(对于您必须“迁移”到的内容),或者如果您希望保留在_消息中,则使用

例如:

import discord
import os


#run the Bot and a message to make sure it ran
client = discord.Client()

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))


@client.event
async def on_message(message):
    if message.author == client.user:
        return
#Detects if someone queues
    if message.content.startswith('-queue duos'):

        await message.channel.send('You have joined the duos queue.')

        await client.wait_for('message', check=lambda m: m.author == message.author and m.content == '-role')

        role = # Define the role you want to add if you haven't before
        await member.add_roles(role)
 

client.run(os.getenv('TOKEN'))

欢迎来到StackOverflow!“我的代码不工作”并没有真正的帮助。到底是什么不起作用?到目前为止你试过什么?你也可以看一看抱歉,因为反应太晚,但我真的很感激!这是非常清楚和详细的,我知道我做错了什么。