Search code examples
python-3.xdiscorddiscord.pybots

I am getting an empty dictionary from my discord bot


I have this code for discord bot:

`from discord.ext import commands
import discord

intents = discord.Intents.default()
intents.members = True

bot = commands.Bot(command_prefix='/', intents=intents)


async def process_message(message):
    user_data = {'username': '', 'link': '', 'command': ''}

    if message.content.startswith('/click'):
        parts = message.content.split(' ')
        user_data['username'] = parts[1]
        user_data['link'] = parts[2]
        user_data['command'] = parts[3]
    return user_data


@bot.event
async def on_ready():
    print('Logged in as {0.user}'.format(bot))


@bot.event
async def on_message(message):
    if message.author == bot.user:
        return

    user_data = await process_message(message)
    print(user_data)

    await bot.process_commands(message)`

In discord I am sending this command /click Ms.Azazel #/battle=2aaaaabaf64e2379 A And the bot gives me empty dictionary like this: {'username': '', 'link': '', 'command': ''} I need this {'username': 'Ms.Azazel', 'link': '#/battle=2aaaaabaf64e2379', 'command': 'A'}

I tryied to modify all code but cant do anything


Solution

  • You didn't enable the message_content intent, so you can't read messages. Hence, message.content.startswith(...) is always False.

    Docs for Message.content:

    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.

    You already managed to enable the members intent, so I hope enabling message_content should be trivial.

    Docs: https://discordpy.readthedocs.io/en/stable/intents.html

    PS consider not using / as a prefix, it's annoying for users because of slash commands that open up a menu in the GUI. You can always create an actual slash command instead of a message command if you really want to use it.