所以基本上我有这个代码:
import discord
import os
bot = commands.Bot(command_prefix = "!")
TOKEN = (os.getenv("TOKEN"))
client = discord.Client()
@client.event
async def on_message(message):
if message.content.startswith('!help'):
embedVar = discord.Embed(
title="Help Page", description="Under development", color=0x00ff00)
await message.channel.send(embed=embedVar)
@client.command()
@commands.has_any_role("Owner")
async def ban (ctx, member:discord.User=None, reason =None):
if member == None or member == ctx.message.author:
await ctx.channel.send("You cannot ban yourself")
return
if reason == None:
reason = "Breaking Rules"
message = f"You have been banned from {ctx.guild.name} for {reason}"
await member.send(message)
# await ctx.guild.ban(member, reason=reason)
await ctx.channel.send(f"{member} is banned!")
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('------')
client.run(TOKEN)
当我运行这个时,我得到了后续错误:
Traceback (most recent call last):
File "main.py", line 4, in <module>
bot = commands.Bot(command_prefix = "!")
NameError: name 'commands' is not defined
有人可以帮我吗?
答案 0 :(得分:0)
在命令的 documentation 中,它声明将其添加到您的导入中:
from discord.ext import commands
答案 1 :(得分:0)
commands
未定义,因为它未在您的代码中导入,将 from discord.ext import commands
放在您的代码顶部。而且,您不需要 discord.Client()
bc commands.Bot()
是它的子类,因此请删除它,因为它是多余的。最后,将 client.command
更改为 bot.command
以使代码工作。