首页 > 解决方案 > 如何定义“@client.command”?

问题描述

所以基本上我有这个代码:

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

有人可以帮帮我吗?

标签: pythondiscord.py

解决方案


在命令文档中,它声明将其添加到您的导入中:

from discord.ext import commands

推荐阅读