【问题标题】:Discord.py Add Role CommandDiscord.py 添加角色命令
【发布时间】:2021-07-06 03:48:46
【问题描述】:

我刚刚开始制作 Discord 机器人。我想创建一个命令,您可以在其中键入 -queue duos 以与其他想要的人一起排队游戏。但是,当我在 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'))

【问题讨论】:

  • 欢迎来到 StackOverflow! “我的代码不起作用”并不是很有帮助。究竟是什么不工作?你都尝试了些什么?你也可以看看how to ask a good question

标签: discord discord.py bots


【解决方案1】:

您的问题是,这没有注册为命令。您应该使用角色命令的特殊条件来执行此操作,并在外部使用@client.command() 注册(对于您必须“迁移”到commands.Bot() 的内容),或者如果您想留在 on_message 然后使用client.wait_for()

例子:

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

【讨论】:

    【解决方案2】:

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

    看看下面的代码:

    from discord.utils import get
    
    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        # detects if someone queues
        if message.content.startswith('-queue duos'):
            role = discord.utils.get(message.guild.roles, id=RoleID) # Define the role
            await message.author.add_roles(role) # Add the role to the author
            await message.channel.send('you have joined the duos queue.')
    

    我们做了什么?

    • 通过discord.utils.get定义角色
    • 确定了消息的作者并分配了角色

    也许还可以再看看docs

    【讨论】:

    • 很抱歉回复晚了,但我真的很感激!它非常清晰和详细,我知道我做错了什么。
    【解决方案3】:

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

    import discord
    from discord.ext import commands
    
    bot = commands.Bot(command_prefix='-')
    botId = 
    
    @bot.event
    async def on_ready():
        await bot.change_presence(activity=discord.Game("with your servers"))
        print("Bot is ready")
    
    @bot.event
    async def on_message(message):
        if message.content == f"<@{botId}>" or message.content == f"<@!{botId}>":
            await message.channel.send("My prefix is -")
        else:
            await bot.process_commands(message)
    
    @bot.command()
    async def role(ctx, member:discord.Member, role:discord.Role, *reason):
        Reason = ' '.join(reason)
        try:
            await member.add_roles(role, reason=Reason)
            await ctx.channel.send("Role added to member")
        except:
            await ctx.channel.send("I suppose I don't have the permission to do that")
    
    bot.run(token)
    

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      相关资源
      最近更新 更多