问题描述
我想创建一个 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 *****"
role = discord.utils.get(server.roles, name="*****")
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 *****':
role = get(message.server.roles, name='*****')
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")
梁书记i