首页 文章

如何制作一个在Python中赋予角色的discord bot?

提问于
浏览
1

我想创建一个discord bot,为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)

1 回答

  • 2
    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的文档是here .

    您还可以使用 discord.ext.commands 扩展名:

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

相关问题