Discord.py 添加角色命令

时间:2021-04-10 13:46:10

标签: discord discord.py bots

我刚刚开始制作 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'))

3 个答案:

答案 0 :(得分:1)

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

看看下面的代码:

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

答案 1 :(得分:0)

因为你定义了但没有调用角色函数。
我想你看到了一个使用 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)

答案 2 :(得分:0)

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

示例:

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