0

I asked for some help to create a command script in which the discord bot would reply to the command with a sentence and randomized percentile. This is the script given to me in question:

import discord
from discord.ext import commands
import random
intents = discord.Intents.all()
bot = commands.Bot(command_prefix=".", intents=intents, help_command=None)
@bot.command()
async def strenght(ctx):
    await ctx.send(f"Your strength is at {random.choice(range(1, 100))} %")
bot.run("")

I already had different parts of the script written, so i just tried my best to implement all the lines into my main script:

import discord
from discord.ext import commands 
import responses
import random


async def send_message(message, user_message):
    try:
        response = responses.get_response(user_message)
        await message.channel.send(response)


    except Exception as e:
        print(e)



def run_discord_bot():
    TOKEN = 'i removed my token'
    intents =  discord.Intents.all()
    bot = commands.Bot(command_prefix=".", intents=intents, help_command=None)
    intents.message_content = True
    client = discord.Client(intents=intents)
    

    @client.event
    async def on_ready():
        print(f'{client.user} is now running!')


    @client.event
    async def on_message(message):
        if message.author == client.user:
            return
        
        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)


        print(f'{username} said: "{user_message}" ({channel})')

        if user_message[0] == '?':
            user_message = user_message[1:]
            await send_message(message, user_message)
        else:
            await send_message(message, user_message)
    
    @bot.command()
    async def strength(ctx):
        await ctx.send(f"Your strength is at {random.choice(range(1,100))} %")
        bot.run("")

    
    
    client.run(TOKEN)

The terminal would then give me successful prints, as in the bot successfully logs in, connects to the gateway, goes online, and even receives the command itself. The problem being that even though it "receives" it, it gives me an error to the lines of " 400 Bad Request (error code: 50006): Cannot send an empty message "

Full Terminal for perspective:

2023-02-24 23:11:13 INFO     discord.client logging in using static token
2023-02-24 23:11:15 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: 4ed57f0b5b4bf87f1a7a1535a6182a52).
hirojiro#3698 is now running!
hirojiro#1357 said: ".strength" (dank-memer)
400 Bad Request (error code: 50006): Cannot send an empty message

I've already asked on Python dedicated forums, yet to get an answer. But i hope to be able to find out what i can change so that the response isn't an "empty message". I'm also open to change my line distribution in my main script if necessary. I want the strength command to work. Thanks a lot!

1 Answers1

0

The problem is that the on_message event is "catching" the message, preventing the command from working. See this post for more on that.

Then, the responses.get_response(user_message) is returning an empty string, which rises the error you are seeing in your console.

Clement Genninasca
  • 732
  • 1
  • 4
  • 14