如何使用bot赋予不和谐角色



我使用的是discord.py库,并使用replit为bot,但我的代码不工作。

我代码:

import os
import discord
from discord.utils import get
client = discord.Client()
@client.event
async def on_ready():
print ("We have logged in as {0.user}".format(client))


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


client.run(os.getenv("TOKEN"))

首先,您使用的是客户端对象,而不是bot。

这是创建一个合适的bot(不是客户端)的方法:

from discord.ext import commands
bot = commands.Bot(command_prefix="!")

如果不阅读discord.py文档,就不能编写反应角色。它涉及到很多东西。如果你想自己编写代码,唯一的方法就是学习和实践。

在Python中这样做的详细方法。

import discord
from discord.ext import commands
bot = commands.Bot(command_prefix="!")
@bot.command()
async def giverole(ctx, member: discord.Member, role_name: str):
"""
A command that adds a role to a member
"""
# Get the role object from the server's roles list
role = discord.utils.get(ctx.guild.roles, name=role_name)

# Check if the role exists
if role is None:
await ctx.send(f"Role '{role_name}' not found")
return

# Add the role to the member
try:
await member.add_roles(role)
await ctx.send(f"Added role '{role_name}' to {member.mention}")
except discord.Forbidden:
await ctx.send("I don't have permission to add roles")
bot.run("your_bot_token")

最新更新