如何制作一个在 Python 中提供角色的不和谐机器人?

2022-01-15 00:00:00 python discord bots discord.py roles

问题描述

我想创建一个 Discord 机器人,在 Python 中为成员分配角色.

I want to create a discord bot that gives roles to members in Python.

我试过了:

@async def on_message(message):
     if message.content == "give me admin"
           role = discord.utils.get(server.roles, name="Admin")
           await client.add_roles(message.author.id, role)


解决方案

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.server.roles, name='Admin')
        await client.add_roles(message.author, role)

我认为这应该可行.discord.py 的文档是这里.

I think this should work. The documentation for discord.py is here.

你也可以使用 discord.ext.commands 扩展:

You could also use the discord.ext.commands extension:

from discord.ext.commands import Bot
import discord

bot = Bot(command_prefix='!')

@bot.command(pass_context=True)
async def addrole(ctx, role: discord.Role, member: discord.Member=None):
    member = member or ctx.message.author
    await client.add_roles(member, role)

bot.run("token")

相关文章