I am making a discord bot on Python with discord.py
I've that problem, when I run the bot, it works, but it receives empty strings instead of messages.
test responses:
import random
def handle_response(message) -> str:
p_message = message.lower()
if p_message == 'hello':
return 'Hey there!'
if p_message == 'roll':
return str(random.randint(1, 6))
if p_message == '!help':
return "`This is a help message that you can modify.`"
# return 'Yeah, I don\'t know. Try typing "!help".'
main:
import bot
if __name__ == '__main__':
bot.run_discord_bot()
problematic code AKA bot:
import discord
import responses
# Send messages
async def send_message(message, user_message, is_private):
try:
response = responses.handle_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
def run_discord_bot():
TOKEN = 'Our_Token'
client = discord.Client(intents=discord.Intents.default())
@client.event
async def on_ready():
print(f'{client.user} is now running!')
@client.event
async def on_message(message):
# Make sure bot doesn't get stuck in an infinite loop
if message.author == client.user:
return
# Get data about the user
username = str(message.author)
user_message = str(message.content)
channel = str(message.channel)
# Debug printing
print(f"{username} said: '{user_message}' ({channel})")
# If the user message contains a '?' in front of the text, it becomes a private message
if user_message[0] == '?':
user_message = user_message[1:] # [1:] Removes the '?'
await send_message(message, user_message, is_private=True)
else:
await send_message(message, user_message, is_private=False)
# Remember to run your bot with your personal TOKEN
client.run(TOKEN)
After every message, the console returns:
Expected behavior:
It should receive the message (not empty string) and if the message is in the responses.py, it should return a message (the bot should send message).
The issue is that you haven't enabled the message_content
intent - you need to set that to be able to read message content.
intents = discord.Intents.default()
intents.message_content = True # explicitly enable the message content intents
client = discord.Client(intents=intents)
You will also need to enable this intent for your bot/application in the Discord Developer portal.
message_content
intent is on the first page of the docs.