在discord.py中,当我使用超过1`on_message`时都无法工作,只有最后一个起作用

发布于 2025-01-31 11:10:53 字数 1509 浏览 3 评论 0原文

这是我的代码,on_message在使用两次时不起作用,只有第二个正在使用。请帮我。

async def on_message(message):<br>
  if message.content.startswith('-coinflip'):<br>
    embedVar = discord.Embed(<br>
      title="Toss",<br>
      description=(f'You got {random.choice(heads_tails)}'),<br>
      color=(0xFF0000))<br>
    print(f'-coinflip command used by {message.author}')<br>
    await message.channel.send(embed=embedVar)<br>

@client.event<br>
async def on_message(message):<br>
  if message.content.startswith('-help'):<br>
        embedVar = discord.Embed(<br>
            title="Help arrived!",<br>
            description="So, it looks like you need help, let me help you",<br>
            colour=0xFF0000)<br>
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)<br>
        embedVar.add_field(name="Moderation Commands",<br>
                           value="-help",<br>
                           inline=True)<br>
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)<br>
        embedVar.set_thumbnail(<br>
            url=<br>
            "https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif"<br>
        )<br>
        print(f'-help command used by {message.author}')<br>
        await message.channel.send(embed=embedVar)<br>```

This is my code, and on_message is not working when used twice, only the 2nd one is working. Please help me.

async def on_message(message):<br>
  if message.content.startswith('-coinflip'):<br>
    embedVar = discord.Embed(<br>
      title="Toss",<br>
      description=(f'You got {random.choice(heads_tails)}'),<br>
      color=(0xFF0000))<br>
    print(f'-coinflip command used by {message.author}')<br>
    await message.channel.send(embed=embedVar)<br>

@client.event<br>
async def on_message(message):<br>
  if message.content.startswith('-help'):<br>
        embedVar = discord.Embed(<br>
            title="Help arrived!",<br>
            description="So, it looks like you need help, let me help you",<br>
            colour=0xFF0000)<br>
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)<br>
        embedVar.add_field(name="Moderation Commands",<br>
                           value="-help",<br>
                           inline=True)<br>
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)<br>
        embedVar.set_thumbnail(<br>
            url=<br>
            "https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif"<br>
        )<br>
        print(f'-help command used by {message.author}')<br>
        await message.channel.send(embed=embedVar)<br>```

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2025-02-07 11:10:53

这是我写的答案,但无法发布:

您不能有2个on_message事件侦听器。您可以通过使用使用喜欢这样的合并两个事件听众及其响应:

@bot.event 
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

elif 与(如果)相同;在这种情况下,如果消息的content不是以“ -coinflip”开头,并以“ -help”开头,它将创建并发送 嵌入

我已更换了全功能代码的heads_tails变量:

from discord.ext import commands
import discord
import discord.utils
import random

intent = discord.Intents(messages=True, message_content=True, guilds=True)

bot = commands.Bot(command_prefix="", description="", intents=intent)

heads_tails = ("Heads", "Tails") #Replace this with your sequence

@bot.event
async def on_ready(): #When the bot comes online
    print("It's online.")

@bot.event
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

另外,是“ -help” a MEDERATION 命令?并且,尝试自己搜索和解决此类问题。 stackoverflow不应该是第一个提出此类问题的地方。

Here's the answer I wrote but couldn't post:

You cannot have 2 on_message event listeners. You can merge the two event listeners and their responses by using if...elif like this instead:

@bot.event 
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

elif is the same as else if; in this case, if the message's content doesn't start with "-coinflip" and starts with "-help", it creates and sends an Embed.

I've replaced the heads_tails variable for a fully-functioning code:

from discord.ext import commands
import discord
import discord.utils
import random

intent = discord.Intents(messages=True, message_content=True, guilds=True)

bot = commands.Bot(command_prefix="", description="", intents=intent)

heads_tails = ("Heads", "Tails") #Replace this with your sequence

@bot.event
async def on_ready(): #When the bot comes online
    print("It's online.")

@bot.event
async def on_message(message): #When any message is sent
    if message.content.startswith('-coinflip'):
        embedVar = discord.Embed(
            title="Toss",
            description=f'You got {random.choice(heads_tails)}',
            color=(0xFF0000))
        print(f'-coinflip command used by {message.author}')
        await message.channel.send(embed=embedVar)

    elif message.content.startswith('-help'):
        embedVar = discord.Embed(
            title="Help arrived!",
            description="So, it looks like you need help, let me help you",
            colour=0xFF0000)
        embedVar.add_field(name="Bot Prefix", value="-", inline=False)
        embedVar.add_field(name="Moderation Commands",
                           value="-help",
                           inline=True)
        embedVar.add_field(name="Fun commands", value="-coinflip", inline=True)
        embedVar.set_thumbnail(
            url="https://media.discordapp.net/attachments/923531605660815373/974248483479494686/charizard-mega-charizard-y.gif")
        print(f'-help command used by {message.author}')
        await message.channel.send(embed=embedVar)

Also, is "-help" a moderation command? And, try searching for and solving such problems yourself. StackOverflow shouldn't be the first place to ask such questions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文