首页 > 解决方案 > 基本命令突然不起作用!不和谐.py

问题描述

我不知道我做错了什么以及我改变了什么。突然间,我的不和谐机器人代码不起作用。请帮我。我已经尝试了很多东西,并检查了我的齿轮是否正确加载并且它们是正确的。我也没有收到任何错误。对不起,如果这只是一个我没有看到的愚蠢错误,因为这是我在 Stack Overflow 上的第一个问题。

一个不起作用的命令示例:

@bot.command()
async def test(ctx):
    await ctx.send("help me")

一个不起作用的齿轮:

#Imports
import discord
from discord.ext import commands
import random
import json
from pathlib import Path

#Current Working Directory
cwd = Path(__file__).parents[1]
cwd = str(cwd)

#Secret
secret_file = json.load(open(cwd+('\secrets.json')))

#Setup
class Basic(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print("Bot basic commands are online.\n---------")

    #Greeting
    @commands.command(aliases = ["hello", "hey"], description = "Greets you.")
    async def hi(self, ctx):
        embed = discord.Embed(colour=0x1ABC9C)
        embed.title = "Hey there!"
        embed.description = f"Nice to see you **{ctx.author.mention}**"
        await ctx.send(embed = embed)

#Add Cog
def setup(bot):
   bot.add_cog(Basic(bot))

如果这是一个愚蠢的错误,我是新来的。非常感谢!

PS:我很高兴附上更多代码。

编辑:我的主要代码(如果需要)

Imports
import discord
import os
from discord.ext import commands, tasks
import random
import logging
from itertools import cycle
from discord import user
import asyncio
import time
import json
from pathlib import Path

#---------------------------------------------------------------------

#Define a few things...
choice = random.choice
randint = random.randint

#Current Working Directory
cwd = Path(__file__).parents[0]
cwd = str(cwd)

#Json
secret_file = json.load(open(cwd+('\secrets.json'))) #Secrets

#Json Variables
TOKEN = secret_file["token"]

#---------------------------------------------------------------------

#Setup
bot = commands.Bot(command_prefix = "-", case_insensitive = True, owner_id = 567327597642383420)
bot.remove_command('help')

@bot.event
async def on_ready():

    bot.loop.create_task(change_status())

    await asyncio.sleep(0.5)

    print(f'Logged in as: {bot.user.name}\n---------')
    print(f'Id: {bot.user.id}\n---------')
    print(f"Path: {cwd}\n---------")

#----------------------------------------------------------------------------

#Functions

#Change Status
async def change_status():
    await bot.wait_until_ready()

    #Local Variables
    statuses = [f"| By Kwuartz_", "| -help"]
    statusnum = 0

    #Loop
    while not bot.is_closed():

        #StatusNumChanger
        if statusnum == 0:
            statusnum = 1

        else:
            statusnum = 0



        await bot.change_presence(activity = discord.Game(name = statuses[statusnum]))
        await asyncio.sleep(5)
        print("Status Changed!\n---------")

#-------------------------------------------------------------------------

#Cog Setup
for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        filename = filename[:-3]
        bot.load_extension(f'cogs.{filename}')


print("All cogs have been succesfully loaded and will be online shortly\n---------")

@bot.command()
async def test(ctx):
    print("yes")
    await ctx.send("help me")
    print("command called")

#---------------------------------------------------------------------------

#Events
@bot.event
async def on_member_join(member):
    print(f'{member} has joined the server')

@bot.event
async def on_member_remove(member):
    print(f'{member} has left the server')

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandNotFound):
        embed = discord.Embed(colour=0x1ABC9C)
        embed.title = "**Command Error:**"
        embed.description = f":x: **Why this happened:**\nThe command you specified does not exist."
        await ctx.send(embed = embed)

    else:
        raise error

@bot.event
async def on_message(message):
    if bot.user.mentioned_in(message):
        await message.channel.send("You can type `-help` for more info")

#Token
bot.run(TOKEN)

标签: pythondiscordbotsdiscord.pydiscord.py-rewrite

解决方案


推荐阅读