简体   繁体   English

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

[英]How to make a discord bot that gives roles in Python?

I want to create a discord bot that gives roles to members in Python.我想创建一个不和谐的机器人,为 Python 中的成员提供角色。

I tried this:我试过这个:

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

I think this should work.我认为这应该有效。 The documentation for is here . 的文档在这里

You could also use the discord.ext.commands extension:您还可以使用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")

All you need to do is你需要做的就是

import discord
from discord.utils import get

@client.event
async def on_message(message):
    if message.content == "give me admin":
        member = message.author
        role = get(member.guild.roles, name="Admin")
        await member.add_roles(role)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM