Discord bot 可以检测到消息已发送,但无法读取服务器通道中发送的消息

问题描述 投票:0回答:2

所以我正在制作一个 Discord 机器人,如果有人搞砸了,它可以恢复计数。由于用户不能计数两次,我不得不制作 2 个应用程序,以便机器人可以交替计数,直到计数恢复。

问题:我的机器人能够检测到一条消息已在指定频道中发送,但它无法读取已发送的消息,因此无法检测到计数中的任何错误,因此无法恢复它。

下面是我的全部代码,希望有人能在这里帮助我。


import discord
import asyncio
import random

class Rick(discord.Client):
    def __init__(self):
        intents = discord.Intents.default()
        intents.members = True
        super().__init__(intents=intents)
        self.count = 1
        self.mistake = None
        self.counting_channel_id = 1100778496130830446 # Replace with the ID of the counting channel

    async def on_ready(self):
        print('Rick is ready!')

    async def on_message(self, message):
        print('message:', message.content)
        print(type(message.content))

        print("Received message:", message.content)
        if message.author == self.user:
            return

        if message.channel.id != self.counting_channel_id:
            return

        if message.content.isdigit():
            number = int(message.content)

            if number == self.count:
                self.count += 1
            else:
                self.mistake = self.count
                self.count = 1

                while self.count <= self.mistake:
                    if self.count % 2 == 1:  #alternating count
                        await message.channel.send(f"Rick counting: {self.count}")
                    else:
                        await message.channel.send(f"Morty counting: {self.count}")
                        await self._count_with_morty(self.count)

                    self.count += 1

                await message.channel.send("Count has been revived!")
                self.mistake = None

    async def _count_with_morty(self, number):
        print("Counting with Morty:", number)
        await self.wait_until_ready()

        while True:
            last_message = await self.get_channel(self.counting_channel_id).fetch_message(
                self.get_channel(self.counting_channel_id).last_message_id
            )

            if last_message.content.startswith("Rick counting:"):
                break

            await asyncio.sleep(1)

        await self.get_channel(self.counting_channel_id).send(f"Morty counting: {number}")

class Morty(discord.Client):
    def __init__(self):
        intents = discord.Intents.default()
        intents.members = True
        super().__init__(intents=intents)

    async def on_ready(self):
        print('Morty is ready!')

async def run_bots():
    rick = Rick()
    morty = Morty()
    await asyncio.gather(rick.start('MTEwMDc3NDY5MTQ2OTI3MTE0MA.GUpeUS.FeP_ZRq6GGXrKVyV6jA_8GV6X9Nz3pKIoV2cEE'),
                         morty.start('MTEwMDc3NTczOTA3NjM5NTAzOA.GyRcqx.6S_1cJegA6HNKlKAi8UJH_aZDDZ9EiHx_lZbPg'))

asyncio.run(run_bots())


My bots weren't doing anything so I tried to debug with some print statements, specifically: 

   async def on_message(self, message):
        print('message:', message.content)
        print(type(message.content))

        print("Received message:", message.content)
**message: 
<class 'str'>
Received message:**

我的解读是机器人无法读取频道中发送的内容。

python discord discord.py bots counting
2个回答
0
投票

你面临的问题可能是由于你的机器人结构

所以首先去https://discord.com/developers/applications

选择你的机器人

单击选项按钮

然后转到

bot
向下滚动并启用所有意图


-1
投票

根据 discord.py 文档 您需要调整意图设置。

消息的实际内容。如果 Intents.message_content 未启用,这将始终为空字符串,除非提到机器人或消息是直接消息。

你能试试这个吗?

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.