Search code examples
pythondiscordpycharmdiscord.py

Discord.py bot responds only when it is pinged. Why?


After the newer Discord.py update (version 2.2.2), the bot only responds when it is pinged/mentioned or when replied to it and does not respond to regular message. I checked the discord developer portal and made sure all the intents are enabled. Yet, the bot does not respond. My bot is supposed to be a moderator by deleting them. Here is a brief description of the problem:

  • User posts nsfw word, bot does NOT respond
  • User pings/mentions the bot and posts nsfw word, the bot responds.

Screenshot of the issue: enter image description here

I tried to find a solution in the documentation of discord.py, but did not find anything that helps. I checked the intents in the discord developer portal, and even continuously disabled and enabled them to see if it was just a glitch.

Since I wanted the bot to function well, as it is under testing, I downgraded the discord.py to an older version (1.7.3) and the bot responds in all circumstances. However, the newer discord.py version (2.2.2) contains some more features and enhancements which could be useful for developing the bot.

I have even tried switching from PyCharm to the regular Python IDLE and saw no difference.

I'm attaching a sample code snippet of my bot.

import os
import discord
from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
import time
import datetime

TOKEN = 'YOUR BOT TOKEN HERE'
GUILD = 'GUILD NAME'
badwords = ['BAD WORDS HERE']         ''' all the bad words in the form of a list. Alternatively a textfile can be used'''

client = discord.Client(intents=discord.Intents(messages=True, guilds=True))  ''' discord intents statement'''

@client.event
async def on_ready():
    for guild in client.guilds:
        if guild.name == GUILD:
            break

    print(
        f'{client.user} has successfully connected to the following guild(s):\n'
        f'{guild.name}(id: {guild.id})'
    )
    await client.change_presence(activity=discord.Activity(name='NAME', type=discord.ActivityType.watching))


@client.event
async def on_message(message):

    if message.author == client.user:
        return

    if 'idk' in message.content.lower():
        await message.reply("Why you don't know?")

    ''' Statements Below are for censoring words'''

    for i in badwords:                      
        if ' ' in message.content.lower():
            msg1 = message.content.split()
            for word in msg1:
                if word.lower() == i and len(word) == len(i):
                    await message.channel.send(f'{message.author.mention} Hey whatch yo language!', delete_after=5.0)
                    await message.delete()
                    
        else:
            if i==message.content.lower():
                await message.channel.send(f'{message.author.mention} Hey whatch yo language!', delete_after=5.0)
                await message.delete()

    ''' Below statement censors the @everyone ping'''
    
    if '@everyone' in message.content.lower():
        await message.channel.send(f"{message.author.mention} Don't ping everyone", delete_after=5.0)
        await message.delete()

    ''' This is a command I made to create a custom message'''

    elif '!cust' in message.content.lower():
        custchannel = client.get_channel('CHANNEL ID HERE')       '''must be int, not a number in string'''
        await custchannel.send(message.content[5:])

client.run(TOKEN) 

If anyone knows a solution to this problem, please let me know.

I use PyCharm with Python interpreter to code the bot. Thank you


Solution

  • Your bot seems to be missing the message_content intent needed for reading the guild messages not sent by the own bot or that contains the bot mention.

    This is how you should initialize the Intents:

    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)
    

    Intents.defult() will enable all intents except presences, members and message_content, which is the one you need for reading the messages so you have to "manually" enable it.

    Alternatively you can use Intents.all() to enable all intents:

    client = discord.Client(intents=discord.Intents.all())
    

    If you want any other intent to be disabled, just do intents."intent_name" = False similar to the code in the first code snippet.

    References: