Discord.py 不响应 @client.event 命令

huangapple go评论51阅读模式
英文:

Discord.py not responding to @client.event commands

问题

I am not getting a "ping" response with this code. It was working before, but I am not sure what changed. There are no errors on my end, just no response.

Any feedback is appreciated.

import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("TOKEN")
intents = discord.Intents().all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)

@bot.event
async def on_message(message):
    if message.author == bot.user:  # tells the bot not to respond to itself
        return

@bot.event  # ping-with-latency
async def on_message(message):
    if message.content.startswith(PREFIX + 'ping'):
        await message.channel.send(f'pong! {bot.latency}ms')

@bot.event
async def on_ready():  # display if online/ready
    print("Bot is ready and logged in as {0.user}!".format(bot))

# run bot on server
bot.run(TOKEN)

I have checked all permissions and privileged gateway intents. I know I could be using client.command, but that also doesn't work.

英文:

I am not getting a "ping" response with this code. It was working before, but I am not sure what changed. There are no errors on my end, just no response.

Any feedback is appreciated.

import os
import random
import discord
from dotenv import load_dotenv
from discord.ext import commands

load_dotenv()
PREFIX = os.getenv("PREFIX")
TOKEN = os.getenv("TOKEN")
intents = discord.Intents().all()
bot = commands.Bot(command_prefix=PREFIX, intents=intents)


@bot.event
async def on_message(message):
    if message.author == bot.user:  # tells the bot not to respond to itself
        return

@bot.event  # ping-with-latency
async def on_message(message):
    if message.content.startswith(PREFIX + 'ping'):
        await message.channel.send(f'pong! {bot.latency}ms')

@bot.event
async def on_ready():  # display if online/ready
    print("Bot is ready and logged in as {0.user}!".format(bot))

# run bot on server
bot.run(TOKEN)

I have checked all permissions and privileged gateway intents. I know I could be using client.command, but that also doesnt work.

答案1

得分: 0

你为这些事件定义了两个不同的回调函数 - 这可能是问题所在。只需将作者检查放在主要的 on_message 中。

@bot.event
async def on_message(message):
    if message.author == bot.user:  # 告诉机器人不要回应自己
        return

    if message.content.startswith(PREFIX + 'ping'):
        await message.channel.send(f'pong! {bot.latency}ms')
英文:

You're defining two different callbacks for these events - this is probably the problem. Just put the author check in the main on_message.

@bot.event
async def on_message(message):
    if message.author == bot.user:  # tells the bot not to respond to itself
        return

    if message.content.startswith(PREFIX + 'ping'):
        await message.channel.send(f'pong! {bot.latency}ms')

huangapple
  • 本文由 发表于 2023年2月10日 16:02:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定