【问题标题】:Bot won't detect message using on_message in discord.pyBot 不会使用 discord.py 中的 on_message 检测消息
【发布时间】:2022-07-09 23:11:07
【问题描述】:

从第 35 行开始,机器人应该检测到消息并在控制台中打印,但它不会在控制台中打印消息。

我已尝试寻找解决方案,但我尝试的每个解决方案都不起作用。我做错了吗?

import discord
from discord import channel
from discord import message
from discord.colour import Color
from discord.errors import PrivilegedIntentsRequired
from discord.ext import commands

token = token
client = commands.Bot(command_prefix="<", case_insensitive=True)

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.command()
async def clear(ctx, amount=100):
    await ctx.channel.purge(limit=amount)

@client.command()
async def move(ctx,member: discord.Member=None, VoiceChannel=None):
    try:
        channel = discord.utils.get(ctx.guild.channels, id=int(VoiceChannel))
        if member == None:
            await ctx.message.author.move_to(channel)
        else:
            await member.move_to(channel)
    except Exception as e:
        embed = discord.Embed(
            title = '**ERROR**',
            description = 'e',
            colour = discord.Color.red()
        )
    await ctx.send(embed=embed)

@client.event
async def on_message(message):
     print(message.content)

@client.event
async def on_message(message):
    ctx = await client.get_context(message)
    if ctx.valid:
        await client.invoke(ctx)

client.run(token)

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

    为什么要使用多个 on_message 事件?使用一个:

    @client.event
    async def on_message(message):
        print(message.content)
    
        ctx = await client.get_context(message)
        if ctx.valid:
            await client.invoke(ctx)
    

    另外请记住,您必须启用intents.messages

    【讨论】:

      【解决方案2】:

      这对我不起作用,但我有其他解决方案

      @client.event

      async def on_message(message):
          message = await message.channel.fetch_message(message.id) # gets the message with id
          messageContent = message.content
      

      【讨论】:

        猜你喜欢
        • 2018-10-15
        • 1970-01-01
        • 2018-05-01
        • 2020-08-16
        • 2020-09-16
        • 2021-12-04
        • 2021-07-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多