My Discord bot works but doesn't execute any commands
Hello or good evening, here is the problem. I created a Discord bot, I can run it, it shows as online and I have the message "En marche!" that appears. The problem is that as soon as I run a command like !fishA or another, first, the command does not execute and second, I do not have the message in the terminal proving that the command has been executed.
import discord from discord.ext import commands, tasks intents = discord.Intents.default() intents.typing = False intents.presences = False intents.messages = True bot = commands.Bot(command_prefix='!', intents=intents) global current_command current_command = ***@*** async def on_ready(): print("En marche !") @bot.command(name='fishA') async def fishA(ctx): global current_command current_command = 'A' fish.change_interval(seconds=3.5) fish.start() @bot.command(name='fishB') async def fishB(ctx): global current_command current_command = 'B' fish.change_interval(seconds=3.2) fish.start() @bot.command(name='fishC') async def fishC(ctx): global current_command current_command = 'C' fish.change_interval(seconds=3.0) fish.start() @tasks.loop(seconds=3.0) async def fish(): global running, current_command if not running: return print(f"Running fish command: {current_command}") await bot.get_channel(1212683308635267112).send('/fish') @bot.command(name='stopfish') async def stopfish(ctx): global running running = ***@***d(name='fish') async def fish(ctx, letter: str): global running if letter not in bot.command: await ctx.send(f'Invalid fish command: {letter}') return running = True fish.change_interval(seconds=bot.command[letter]) fish.start() bot.run("") Thanks in advance for your help.
5 answers
-
Hello,
Before getting to the point, I advise you against your own messages—the best way for people to wrongly think someone is interacting with you. In the future, it would be better to correct the initial message so the thread appears without a reply.
Regarding your code, I see that some functions have names containing accented characters, which is a likely cause of your problem. To avoid this pitfall, I also recommend coding in English; it's a good habit for the future.
In response to #5: you can also trace what happens with the logging module, as explained here. This will allow you to see if you go through a function, the values of local variables, etc., as you had done in #4 so we can see where you stand and what remains to debug.
Good luck -
UPDATE, i changed the code, but still nothing. I’m still sharing the code to support you or even see if you spot the slightest error or something.
import discord from discord.ext import commands, tasks import asyncio intents = discord.Intents.default() intents.typing = False intents.presences = False intents.messages = True bot = commands.Bot(command_prefix='/', intents=intents) # The commands and their intervals commands = { 'A': 3.5, 'B': 3.2, 'C': 3.0, 'D': 2.7, 'E': 2.4 } @bot.event async def on_ready(): print("En marche !") # Dictionary to store running tasks running_tasks = {} # The function that sends the message async def send_message(channel, command): await channel.send(f'/fish {command}') # The fish commands A, B, C, D, ***@***d(name='fishA') async def fishA(ctx): interval = commands.get('A') await start_fishing(ctx, 'A', interval) @bot.command(name='fishB') async def fishB(ctx): interval = commands.get('B') await start_fishing(ctx, 'B', interval) @bot.command(name='fishC') async def fishC(ctx): interval = commands.get('C') await start_fishing(ctx, 'C', interval) @bot.command(name='fishD') async def fishD(ctx): interval = commands.get('D') await start_fishing(ctx, 'D', interval) @bot.command(name='fishE') async def fishE(ctx): interval = commands.get('E') await start_fishing(ctx, 'E', interval) # Function to start the fishing task with the specified interval async def start_fishing(ctx, command, interval): if command not in running_tasks: running_tasks[command] = bot.loop.create_task(fish_loop(ctx, command, interval)) await ctx.send(f'La commande fish{command} a été démarrée avec un intervalle de {interval} secondes.') else: await ctx.send(f'La commande fish{command} est déjà en cours.') # Function to loop sending the command at the specified interval async def fish_loop(ctx, command, interval): channel = ctx.channel while True: await send_message(channel, command) await asyncio.sleep(interval) # The code that stops the tâ***@***d(name='stopfish') async def stopfish(ctx): global running_tasks for task in running_tasks.values(): task.cancel() running_tasks = {} await ctx.send('Toutes les tâches de pêche ont été arrêtées.') # Bot startup bot.run -
Hi, while looking for the warning:
https://duckduckgo.com/?t=ffab&q=privileged+message+content+intent+is+missing&ia=web
Which mostly indicates to add
intent.message_content = True
And the parentheses are missing in bot.run in your code, so of course nothing happens.
-
Good evening, after trying nothing changed. I’m sending my modified code but it still doesn’t work. The terminal always returns:
2024-03-02 22:07:16 WARNING discord.ext.commands.bot Privileged message content intent is missing, commands may not work as expected.
2024-03-02 22:07:16 INFO discord.client logging in using static token
2024-03-02 22:07:16 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: 22d68492fd3f2157e069224e21b25271).
Connected as BotFish#5865
Message received: Bot prêt à pêcher !
The problem is that everything works; when I enter a message the bot reads it and replies "Je suis un bot Discord et je réponds à tous les messages !" but when it comes to a command, nothing happens and it returns Message recu: <empty>. I don’t understand, I’m stuck, I’ll check if there were modifications to the Discord.py library
Have a good evening
</empty> -
import discord import asyncio from discord import Intents from discord.ext import commands # Create an Intents object to define intents intents = Intents( messages=True, guilds=True, reactions=True, presences=True, members=True ) # Initialize the bot with the command prefix bot = commands.Bot(command_prefix='/', intents=intents) # Discord channel ID ID_DE_CANAL = # Token bot Discord TOKEN = # Default interval for fishing in seconds INTERVALLE_PAR_DEFAUT = 3.5 # Dictionary to store specified intervals for each command intervalles = { "/fishA": 3.5, # Example: Fish every 3.5 seconds with command /fishA "/fishB": 3.2, # Example: Fish every 3.2 seconds with command /fishB "/fishC": 3, # Example: Fish every 3 seconds with command /fishC "/fishD": 2.7, # Example: Fish every 2.7 seconds with command /fishD "/fishE": 2.4, # Example: Fish every 2.4 seconds with command /fishE } async def pêcher(channel, interval): while True: await asyncio.sleep(interval) await channel.send('/fish') # Event triggered when the bot is ready***@*** async def on_ready(): print(f'Connected as {bot.user}') channel = bot.get_channel(int(ID_DE_CANAL)) await channel.send('Bot prêt à pêcher !') # Fishing commands spé***@***d() async def fishA(ctx): interval = intervalles['/fishA'] await pêcher(ctx.channel, interval) @bot.command() async def fishB(ctx): interval = intervalles['/fishB'] await pêcher(ctx.channel, interval) @bot.command() async def fishC(ctx): interval = intervalles['/fishC'] await pêcher(ctx.channel, interval) @bot.command() async def fishD(ctx): interval = intervalles['/fishD'] await pêcher(ctx.channel, interval) @bot.command() async def fishE(ctx): interval = intervalles['/fishE'] await pêcher(ctx.channel, interval) # Command to start fishing***@***d() async def fish(ctx, type): if type in intervalles: interval = intervalles[type] await pêcher(ctx.channel, interval) else: await ctx.send("Type de pêche invalide !") # Command to stop fishing***@***d() async def stopfish(ctx): await ctx.send('Pêche arrêtée.') for task in asyncio.all_tasks(): task.cancel() # Start the bot bot.run(TOKEN)I have modified the code again, but still nothing. Don’t worry, the problem is not at the level of ID_DE_CANAL and TOKEN, I deliberately removed them.
Bonne soirée
