为什么此程序在discord.py中检索消息时不出错

2024-04-26 01:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图检查在discord中发送的消息是否包含特定内容(最终目标是反咒骂机器人),并且每当发送消息时,我都会收到此错误

Bot is ready. Ignoring exception in on_message Traceback (most recent call last):   File "C:\Users\Coran\AppData\Local\Programs\Python\Python37\lib\site-packages\discord\client.py", line 251, in _run_event
    await coro(*args, **kwargs)   File "C:\Users\Coran\OneDrive\Desktop\Bully bot\bullybot.py", line 32, in on_message
    if message.content.includes("E"): NameError: name 'message' is not defined

节目

import discord 
import random 
from discord.ext import commands

client = commands.Bot(command_prefix = ".")

@client.event 
async def on_ready():
    print ("Bot is ready.")

@client.command() 
async def ping(ctx):
    await ctx.send(f'Pong! {client.latency * 1000}ms')

@client.command(aliases=['8ball']) 
async def _8ball(ctx, *, question):
    responses = ['It is certain.',
                 'It is decidedly so.',
                 'Yes - definitely.',
                 'Reply hazy, try again.',
                 "Don't count on it.",
                 'Outlook not so good.',
                 'Very doubtful.']
    await ctx.send(f'Question: {question}\nAnswer: {random.choice(responses)}')

@client.event 
async def on_message(ctx):
    print ("message sent.")
@client.event 
async def on_message(ctx):
if message.content.includes("E"):
    print ("Yay")

当发送包含E的消息时,代码应在控制台中打印yay,如果消息不包含E,则不打印任何内容。 但是,无论发送什么,我都只会收到此错误


Tags: inimportclientevent消息messageasyncis