简体   繁体   中英

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.

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:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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