首页 > 解决方案 > 我对使用 python 制作不和谐的机器人非常陌生,由于某种原因,机器人不响应命令

问题描述

如标题中所述,我是 Python 新手(一般编程),我尝试制作一个机器人,但是该机器人不响应命令。我关注/浏览了多个 youtube 教程和文章,但我找不到解决问题的方法。

import discord
from discord.ext.commands import Bot

bot = Bot(".")

@bot.event
async def on_ready():
    print("kram is now online")
    await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))

@bot.event
async def on_message(message):
    if message.author == bot.user:

        @bot.command(aliases=["gp"])
        async def ghostping(ctx, amount=2):
            await ctx.send("@everyone")
            await ctx.channel.purge(limit = amount)

        @bot.command()
        async def help(ctx):
            await ctx.send("As of right now, .gp is the only working command.")

bot.run("I'm hiding my token")

标签: pythonpython-3.xdiscorddiscord.py

解决方案


嘿,你为什么不试试这个,而不是同样的事情,但我删除了 on 消息

import discord
from discord.ext.commands import Bot

bot = Bot(".")

@bot.event
async def on_ready():
     print("kram is now online")
     await bot.change_presence(activity=discord.Game(name="This bot is a WIP"))


@bot.command(aliases=["gp"])
async def ghostping(ctx, amount=2):
      await ctx.send("@everyone")
      await ctx.channel.purge(limit = amount)

@bot.command()
async def help(ctx):
      await ctx.send("As of right now, .gp is the only working command.")

bot.run("I'm hiding my token")

这应该在使用 cogs 时起作用,并且当您有命令时,无需将其放入 on_message 事件中。我建议你看这个系列(刚开始时真的很有帮助):

https://www.youtube.com/watch?v=yrHbGhem6I4&list=UUR-zOCvDCayyYy1flR5qaAg


推荐阅读