Search code examples
pythondiscorddiscord.pybotscounting

Discord bot can detect that a message has been sent, but cannot read the message sent in a server channel


So I'm making a Discord bot that can revive count if someone messes up. Since a user cannot count twice, I've had to make 2 applications so that the bots can count alternatively until the count has been revived.

PROBLEM: My bots are able to detect that a message has been sent in the designated channel, but it's unable to read the message that has been sent hence making them unable to detect any mistake made in the count and hence unable to revive it.

Below is my entire code anyways, hope someone can help me out here.


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('token'),
                         morty.start('token'))

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:**

My reading of this is that the bot is not able to read what has been sent in the channel.


Solution

  • According to discord.py documentation you need to adjust the intents settings.

    The actual contents of the message. If Intents.message_content is not enabled this will always be an empty string unless the bot is mentioned or the message is a direct message.

    Could you try this?