如何让不和谐机器人给我一个角色

问题描述 投票:0回答:1

我创建了自己的不和谐机器人,它拥有服务器上的所有权限。我需要一个 Python 脚本来让它为我提供一个角色,但我真的不知道如何做。

python discord
1个回答
1
投票

您提到了Python,所以我假设您正在使用discord.py 库,但是您应该提及您在问题中使用的库并包含一些代码。

这是您的选择。

  1. 使用
    on_message
    事件:您可以通过侦听 on_message 事件并检查消息内容是否与特定命令匹配(例如,“给我管理员”)来实现此目的。以下是如何做到这一点的示例:
import discord
from discord.utils import get

client = discord.Client()

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content == 'give me admin':
        role = get(message.guild.roles, name='Admin')
        await message.author.add_roles(role)

# Replace 'your_bot_token' with your actual bot token
client.run('your_bot_token')
  1. 使用
    discord.ext.commands
    扩展:另一种方法是使用discord.ext.commands扩展,它提供了一种更简洁的方式来处理命令。这是一个例子:
from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='!')

@bot.command()
async def addrole(ctx, role: discord.Role, member: discord.Member = None):
    member = member or ctx.message.author
    await member.add_roles(role)
    await ctx.send(f"{member.mention} has been given the {role.name} role.")

# Replace 'your_bot_token' with your actual bot token
bot.run('your_bot_token')

我建议你在这里查看discord.py库文档:discordpy.readthedocs.io.

© www.soinside.com 2019 - 2024. All rights reserved.