【发布时间】:2021-07-26 08:14:51
【问题描述】:
我最近刚刚尝试清理我的 main.py 文件,因为其中有很多代码。我想将每个命令存储到一个文件中。我从清除/清除命令开始。每当我输入>clean 时,我都会收到错误消息“找不到命令”。我该如何解决?一些 main.py 和 clear.py 的代码如下。任何帮助,将不胜感激! :D
MAIN.PY
import discord
from discord import User
import os
from keep_alive import keep_alive
from discord.ext import commands
import random
import requests
import json
import asyncio
from discord.ext.commands import MissingPermissions
from discord.ext.commands import Bot, Greedy
from discord.ext.commands import CommandNotFound
bot=commands.Bot(command_prefix = '>')
bot.remove_command("help")
@bot.event
async def on_ready():
print('The bot is online')
await bot.change_presence(activity=discord.Game('>help | Bot made by ColeTMK'))
CLEAR.PY
from discord.ext import commands
import asyncio
from discord.ext.commands import MissingPermissions
bot=commands.Bot(command_prefix = '>')
class Clear():
def __init__(self, bot):
self.bot = bot
@bot.command()
@commands.has_permissions(manage_messages=True)
async def clear(ctx, amount=5):
await ctx.channel.purge(limit=amount+1)
embed=discord.Embed(title="Clear Messages", description=f'{amount} messages were deleted!', color=0x00FFFF)
await ctx.send(embed=embed)
await asyncio.sleep(3)
await ctx.channel.purge(limit=1)
@clear.error
async def clear_error(ctx, error):
if isinstance(error, MissingPermissions):
await ctx.send(f"{ctx.author.mention}, Sorry, you do not have permission to do that!")
def setup(bot):
bot.add_cog(Clear(bot))
【问题讨论】:
-
这能回答你的问题吗? How do I use cogs with discord.py?
标签: python discord discord.py