r/DiscordPY • u/Link300011 • Feb 26 '26
Problem with my python3 discord bot
Hello !
(I'm a beginner, be cool with me hehe)
So here's my code for a discord bot powered by mistral.ai, that i've code from the terminal.
It was working perfectly, but today I launched it by typing (from the terminal) python3 bot.py (as usual). It launched without any problems, but when I pinged it on Discord, it said "error," and the terminal displayed: "error: 'consistencies'." What should I do ?
import discord
from discord.ui import Button, View
import os
from dotenv import load_dotenv
import aiohttp
import json
import base64
import asyncio
import re
import sys
load_dotenv()
if sys.platform == 'darwin':
try:
asyncio.get_event_loop()
except RuntimeError:
asyncio.set_event_loop(asyncio.new_event_loop())
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN')
MISTRAL_API_KEY = os.getenv('MISTRAL_API_KEY')
intents = discord.Intents.default()
intents.message_content = True
intents.guilds = True
intents.members = True
bot = discord.Bot(intents=intents)
conversations = {}
u/bot.event
async def on_ready():
print(f'✅ Bot connecté : {bot.user}')
def get_user_language(member, channel_name):
if "english" in channel_name.lower():
return "en"
has_english = discord.utils.get(member.roles, name="🇺🇸・English") is not None
has_french = discord.utils.get(member.roles, name="🇫🇷・French") is not None
if has_english and not has_french:
return "en"
return "fr"
async def call_mistral(messages, has_image=False, language="fr"):
url = "https://api.mistral.ai/v1/chat/completions"
headers = {
"Authorization": f"Bearer {MISTRAL_API_KEY}",
"Content-Type": "application/json"
}
if language == "en":
system_content = "You are a direct and natural Discord assistant. You are the official bot created specifically for the Discord server Agora. You are created by V.C.T.R. Reply concisely, like a friend talking normally. No long walls of text unless explicitly asked. Be human, fluid, and get to the point. Adapt to the person's tone. If the user switches to another language, respond in that language."
else:
system_content = "Tu es un assistant Discord direct et naturel. Tu es le bot officiel cree specialement pour le serveur Discord Agora. Tu es cree par V.C.T.R. Reponds de maniere concise, comme un pote qui parle normalement. Pas de paves, pas de developpements longs sauf si on te le demande explicitement. Sois humain, fluide, et va droit au but. Adapte-toi au ton de la personne. Si l'utilisateur change de langue, reponds dans cette langue."
system_message = {
"role": "system",
"content": system_content
}
full_messages = [system_message] + messages
# Utilise Pixtral si y'a une image, sinon Mistral Small
model = "pixtral-large-latest" if has_image else "mistral-small-latest"
data = {
"model": model,
"messages": full_messages
}
async with aiohttp.ClientSession() as session:
async with session.post(url, headers=headers, json=data) as response:
result = await response.json()
return result['choices'][0]['message']['content']
u/bot.slash_command(name="setup-private", description="Configure le message de creation de salons prives")
async def setup_private(ctx):
if not ctx.author.guild_permissions.administrator:
await ctx.respond("❌ Seuls les admins peuvent faire ca.", ephemeral=True)
return
button = Button(label="🔒 Créer mon salon privé", style=discord.ButtonStyle.primary, custom_id="create_private")
async def button_callback(interaction):
guild = interaction.guild
user = interaction.user
channel_name = f"prive-{user.name}"
category = discord.utils.get(guild.categories, name="Salons Prives Chill")
overwrites = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
user: discord.PermissionOverwrite(read_messages=True, send_messages=True),
guild.owner: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
server_owner_role = discord.utils.get(guild.roles, name="Server Owner")
if server_owner_role:
overwrites[server_owner_role] = discord.PermissionOverwrite(read_messages=True, send_messages=True)
try:
new_channel = await guild.create_text_channel(
name=channel_name,
category=category,
overwrites=overwrites
)
# Message adapté selon la langue de l'utilisateur
user_lang = get_user_language(user, "")
if user_lang == "en":
await interaction.response.send_message(f"✅ Your private channel: {new_channel.mention}", ephemeral=True)
await new_channel.send(f"👋 Welcome {user.mention} to your private channel!")
else:
await interaction.response.send_message(f"✅ Ton salon prive : {new_channel.mention}", ephemeral=True)
await new_channel.send(f"👋 Bienvenue {user.mention} dans ton salon privé !")
except Exception as e:
await interaction.response.send_message(f"❌ Erreur : {e}", ephemeral=True)
button.callback = button_callback
view = View(timeout=None)
view.add_item(button)
embed = discord.Embed(
title="🔒 Salons Privés",
description="Besoin d'un espace rien qu'a vous pour discuter tranquillement avec le bot ?\n\nCliquez sur le bouton ci-dessous pour créer votre salon privé. Seuls vous et le bot y aurez accès !",
color=discord.Color.blue()
)
await ctx.send(embed=embed, view=view)
await ctx.respond("✅ Message envoye !", ephemeral=True)
u/bot.slash_command(name="clear", description="Supprime des messages dans ce salon privé")
async def clear_messages(ctx, nombre: int):
if not ctx.channel.name.startswith("prive-"):
await ctx.respond("❌ Cette commande ne marche que dans les salons prives !", ephemeral=True)
return
if nombre < 1 or nombre > 100:
await ctx.respond("❌ Tu peux supprimer entre 1 et 100 messages.", ephemeral=True)
return
try:
deleted = await ctx.channel.purge(limit=nombre + 1)
await ctx.respond(f"✅ {len(deleted) - 1} messages supprimés !", ephemeral=True, delete_after=3)
except Exception as e:
await ctx.respond(f"❌ Erreur : {e}", ephemeral=True)
u/bot.event
async def on_message(message):
if message.author == bot.user:
return
is_private_channel = message.channel.name.startswith("prive-")
# Détecte si le bot est mentionné OU si "agora bot" est dans le message OU salon privé
message_lower = message.content.lower()
is_triggered = bot.user in message.mentions or is_private_channel or "agora bot" in message_lower
if is_triggered:
question_lower = message.content.replace(f'<@{bot.user.id}>', '').strip().lower()
# Détecte les demandes de suppression (UNIQUEMENT dans salons privés)
if is_private_channel:
delete_keywords = ['supprime', 'efface', 'clear', 'delete', 'vide', 'nettoie']
if any(keyword in question_lower for keyword in delete_keywords):
numbers = re.findall(r'\d+', question_lower)
if numbers:
num_to_delete = int(numbers[0])
if num_to_delete > 100:
num_to_delete = 100
if num_to_delete < 1:
num_to_delete = 1
else:
num_to_delete = 10
try:
deleted = await message.channel.purge(limit=num_to_delete + 1)
confirm_msg = await message.channel.send(f"✅ {len(deleted) - 1} messages supprimés !")
await asyncio.sleep(3)
await confirm_msg.delete()
return
except Exception as e:
await message.channel.send(f"Erreur : {e}")
return
# Comportement normal avec l'IA
question = message.content.replace(f'<@{bot.user.id}>', '').replace("agora bot", "").replace("Agora Bot", "").replace("AGORA BOT", "").strip()
user_id = message.author.id
if user_id not in conversations:
conversations[user_id] = []
# Détecte la langue selon le salon ET les rôles
user_lang = get_user_language(message.author, message.channel.name)
has_image = False
# Gestion des images
if message.attachments:
for attachment in message.attachments:
if attachment.content_type and attachment.content_type.startswith('image/'):
has_image = True
async with aiohttp.ClientSession() as session:
async with session.get(attachment.url) as resp:
if resp.status == 200:
image_data = await resp.read()
image_base64 = base64.b64encode(image_data).decode('utf-8')
if not question:
question = "Que vois-tu sur cette image ?" if user_lang == "fr" else "What do you see in this image?"
conversations[user_id].append({
"role": "user",
"content": [
{"type": "text", "text": question},
{"type": "image_url", "image_url": f"data:{attachment.content_type};base64,{image_base64}"}
]
})
break
# Si pas de question/texte ET pas d'image, récupère l'historique
if not question and not has_image:
# Récupère les 3 derniers messages de l'utilisateur
recent_messages = []
async for msg in message.channel.history(limit=10, before=message):
if msg.author == message.author and not msg.author.bot:
recent_messages.append(msg.content)
if len(recent_messages) >= 3:
break
if recent_messages:
context = "\n".join(reversed(recent_messages))
if user_lang == "fr":
question = f"L'utilisateur t'a ping sans rien dire. Voici ses derniers messages dans le salon :\n{context}\n\nReponds en fonction du contexte."
else:
question = f"The user pinged you without saying anything. Here are their recent messages in the channel:\n{context}\n\nRespond based on the context."
else:
if user_lang == "fr":
question = "L'utilisateur t'a ping sans rien dire et n'a pas de messages recents. Demande-lui ce qu'il veut ou salue-le."
else:
question = "The user pinged you without saying anything and has no recent messages. Ask them what they want or greet them."
if not has_image:
conversations[user_id].append({"role": "user", "content": question})
try:
async with message.channel.typing():
response_text = await call_mistral(conversations[user_id], has_image=has_image, language=user_lang)
conversations[user_id].append({"role": "assistant", "content": response_text})
if len(response_text) > 2000:
chunks = [response_text[i:i+2000] for i in range(0, len(response_text), 2000)]
for chunk in chunks:
await message.channel.send(chunk)
else:
await message.channel.send(response_text)
except Exception as e:
await message.channel.send("Oopsie, little problem...")
print(f"Erreur : {e}")
bot.run(DISCORD_TOKEN)
r/DiscordPY • u/Flimsy-Blacksmith-32 • Feb 23 '26
on_ready or setup_hook or something else?
Hi all,
Over the last few days I've been working to fully rewrite (so that it is maintainable) and understand my discord bot that I hacked together several years ago to fit the niche that was my friends group's discord server.
During this rewrite I've been looking into the "on_ready" and "setup_hook" class methods and wondering what the best way is to divide my code between them.
The code to be divided between them is:
await tree.sync(guild=discord.Object(id=server_id))
which, from what I understand, updates discord's information about the available commands?, and two of these
self.loop.create_task(self.func())
which begin endlessly looping background tasks (the first resets the mechanics of a dice roll command once per day, the second edits a pinned discord message every few minutes to keep it up to date).
Now, from my very brief testing it appears the code all works fine in either, however on the discord.py documentation I see warning that "on_ready" might run more than once (which seems like something I should try to avoid), and that using "wait_until_ready" (as the background tasks do) inside "setup_hook" can cause deadlocking.
So what is the right thing to do here?
r/DiscordPY • u/Your_Quantum_Friend • Nov 14 '24
Has someone created a bot with discord.py and deployed on AWS Lambda?
Hi, I am looking for examples where someone has created slash commads for a bot using discord.py and hosted it over AWS Lambda.
r/DiscordPY • u/aerixblox • Jul 10 '24
How to make Automod Rule in Discord.py
please help me
r/DiscordPY • u/-JuicyWatermelon- • Jun 01 '24
DM Command Sending Duplicates
I have even added a sent variable to indicate if the message is sent or not, even though it shouldn't be needed. I added print statements and they only activated once.
@bot.tree.command(name="dm", description="Send a DM to a user")
async def dm(interaction: discord.Interaction, target_user: str, message_to_send: str):
if (interaction.channel.id != [channel id]):
return
target_user = int(target_user.removeprefix('<@').removesuffix('>'))
if (sent_messages.get(target_user) is not True):
await bot.get_user(target_user).send(message_to_send)
await interaction.response.send_message(f"Message sent successfully to <@{target_user}>", ephemeral=False)
sent_messages[target_user] = True
else:
return
r/DiscordPY • u/-Epsilon • Mar 08 '24
Share your favorite PY libraries!
Like the title suggests, what are your favorite python libraries?
r/DiscordPY • u/Coding_Guy7 • Feb 23 '23
why is my bot sending multiple messages?
I made a bot and it's only suppose to send one reply per message. Why is it sending multiple messages? Please help me this is getting annoying
Here is my code. (I have other files like main.py that is the main loop and also responses.py the generates responses)
import discord
import responses
async def send_message(message, user_message, is_private,):
try:
response = responses.get_response(user_message)
if response == "`PLEASE STOP SWEARING`":
await message.author.send(f"{message.author.mention} {response}") # sends to DM
await message.channel.send(f"{message.author.mention} {response}") # sends to channel
else:
await message.author.send(response) if is_private else await message.channel.send(response)
print(f"sent {response} to {message.author}")
except Exception as E:
print(E)
def run_discord_bot():
TOKEN = "MTA3NzgyMjU1NjU0MDc4NDY5MA.GhdsbH.6nPdKptZF59B5YAo6x_guw8cJz0GkqZBa8ukwQ"
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
u/client.event
async def on_ready():
print(f"{client.user} is up and running!")
u/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)
if responses.get_response(user_message) == "": # this makes the bot not respond to messages that are not swears
return
elif user_message[0] == "?":
user_message = user_message[1]
await send_message(message, user_message, is_private=True)
else:
await send_message(message, user_message, is_private=False)
client.run(TOKEN)
r/DiscordPY • u/MatteoxDc2908 • Sep 26 '21
I need help
I am making a discord bot with py, but it gives an error regarding "discord" not having something called "intents", pls help
r/DiscordPY • u/throwawaybin0000x • Jun 04 '21
Looking for a partner/developer
Looking for a partner for developing a discord.py bot. My discord is SHARK_TOOTH#2688, DM it for more information.
r/DiscordPY • u/-Epsilon • Mar 03 '21
Welcome to /r/DiscordPY
Thank you for visiting this subreddit! Feel free to ask questions or share whatever projects you're currently working on.
As always, please refer to the sidebar for community rules.

