我对discord API不熟悉,而且我无法弄清楚为什么我的命令无法识别。我已经阅读了文档,但我不确定在哪里查看。任何帮助,将不胜感激。不要介意硬编码列表。我打算将来改变它。现在我只想确保它有效。
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
Client = discord.Client()
client = commands.Bot(command_prefix = "!")
@client.event
async def on_ready():
print("Bot online!")
@client.command(pass_context=True)
async def add_roles(member, *roles):
if message.content.startswith("!role"):
role_list = ["CS101", "CS492", "CS360", "CS213", "CS228", "CS401", "CS440", "CS450", "CS480", "CS410", "CS420", "CS430", "CS108", "CS111", "CS226", "CS312", "CS405", "CS413", "CS435", "CS499", "CS250", "CS475", "CS445"]
entered_role = message.content[6:].upper()
role = discord.utils.get(message.server.roles, name=entered_role)
if role is None or role.name not in role_list:
# If the role wasn't found by discord.utils.get() or is a role that we don't want to add:
await client.send_message(message.channel, "Role doesn't exist.")
return
elif role in message.author.roles:
# If they already have the role
await client.send_message(message.channel, "You already have this role.")
else:
try:
await client.add_roles(message.author, role)
await client.send_message(message.channel, "Successfully added role {0}".format(role.name))
except discord.Forbidden:
await client.send_message(message.channel, "I don't have perms to add roles.")
@client.command(pass_context=True)
async def remove_roles(member, *roles):
if message.content.startswith("!unassign"):
role_list = ["CS101", "CS492", "CS360", "CS213", "CS228", "CS401", "CS440", "CS450", "CS480", "CS410", "CS420", "CS430", "CS108", "CS111", "CS226", "CS312", "CS405", "CS413", "CS435", "CS499", "CS250", "CS475", "CS445"]
roles_cleared = True
for r in role_list:
# Check every role
role = discord.utils.get(message.server.roles, name=r)
if role in message.author.roles:
# If they have the role, get rid of it
try:
await client.remove_roles(message.author, role)
except discord.Forbbiden:
await client.send_message(message.channel, "I don't have perms to remove roles.")
roles_cleared = False
break
if roles_cleared:
await client.send_message(message.channel, "Roles successfully cleared.")
client.run("mytoken")
答案 0 :(得分:-1)
在功能中你需要有ctx变量
M
我认为......
告诉你是否对你有帮助