This repository has been archived on 2022-06-13. You can view files and clone it, but cannot push or open issues or pull requests.
KassouBot/src/main.py

106 lines
3.6 KiB
Python
Raw Permalink Normal View History

2021-05-28 13:52:37 +02:00
print("Chargement des extensions & librairie...", end = " ")
2020-11-29 11:39:41 +01:00
2021-05-28 14:13:14 +02:00
import discord
from os import listdir, rename, getcwd
2021-05-28 14:03:34 +02:00
from discord_slash import SlashCommand
2020-11-29 11:39:41 +01:00
from discord.ext import commands
2021-08-17 11:53:38 +02:00
from utils.core import load, addReaction
2021-08-22 15:47:40 +02:00
from utils.page import listReaction
2021-08-02 12:50:38 +02:00
keys = load(["PREFIX", "TOKEN_DISCORD", "DEACTIVATE"])
2021-07-28 03:17:37 +02:00
customPrefix = keys["PREFIX"]
2020-11-29 11:39:41 +01:00
2021-05-07 16:44:47 +02:00
client = commands.Bot(command_prefix = customPrefix, case_insensitive = True, intents = discord.Intents.all())
slash = SlashCommand(client, sync_commands = True)
2020-11-29 11:39:41 +01:00
client.cogs_folder = "cogs"
2021-08-02 12:37:07 +02:00
if keys["DEACTIVATE"] != "None":
path = getcwd()
2021-08-02 12:37:07 +02:00
for file in keys["DEACTIVATE"]:
try:
rename(f"{path}/{client.cogs_folder}/{file}.py", f"{path}/{client.cogs_folder}/-{file}.py") # désactivation
except:
2021-08-02 12:50:08 +02:00
print(f"No file {file} found, check your \"DEACTIVATE\" variable.")
exit(1)
for file in listdir(client.cogs_folder):
2021-06-10 11:15:49 +02:00
if file.endswith(".py") and file.startswith("-") == False:
client.load_extension(f"{client.cogs_folder}.{file[:-3]}")
2021-05-28 13:52:37 +02:00
print("Terminé !")
2020-11-29 11:39:41 +01:00
@client.event
async def on_connect():
2021-08-17 11:35:25 +02:00
"""Triggered when the bot is connected to Discord"""
2021-09-02 04:06:15 +02:00
print("Connecté !")
2020-11-29 11:39:41 +01:00
@client.event
2021-06-24 13:17:20 +02:00
async def on_disconnect():
2021-08-17 11:35:25 +02:00
"""Triggered when the bot is disconnected to Discord"""
2021-09-02 04:06:15 +02:00
print("Déconnecté.")
2021-06-24 13:17:20 +02:00
@client.event
2021-06-24 22:48:10 +02:00
async def on_resumed():
2021-08-17 11:35:25 +02:00
"""Triggered when the bot is reconnected to Discord"""
2021-09-02 04:06:15 +02:00
print("Reconnecté !")
2021-06-24 22:48:10 +02:00
@client.event
async def on_ready(update_changePresence = False):
2021-08-17 11:35:25 +02:00
"""Triggered when the bot is ready to operate"""
await client.change_presence(status = discord.Status.online, activity = discord.Activity(name = f"{customPrefix}help · {len(client.guilds)} serveur{'s' if len(client.guilds) > 1 else ''}", type = discord.ActivityType.playing))
if update_changePresence == False:
print("Bot prêt.")
@client.event
async def on_guild_join(_):
"""Triggered when the bot join a new guild"""
await on_ready(True)
@client.event
async def on_guild_remove(_):
"""Triggered when the bot left a guild"""
await on_ready(True)
2020-11-29 11:39:41 +01:00
@client.event
async def on_command_error(ctx, error):
2021-08-17 11:35:25 +02:00
"""Triggered when a command enconter an error"""
2021-08-22 03:55:22 +02:00
if isinstance(error, commands.errors.CommandNotFound): # si commande inconnu
2021-08-19 11:00:52 +02:00
if ctx.message: # si pas une commande slash
2021-08-23 12:38:13 +02:00
if not ctx.invoked_with.startswith(f"{customPrefix}"):
await addReaction(ctx.message, 1) # ajout réaction
2021-08-22 03:55:22 +02:00
return
2021-08-19 11:00:52 +02:00
else:
2021-08-23 12:46:55 +02:00
await addReaction(ctx.message, 2) # ajout réaction
2021-08-19 11:00:52 +02:00
raise error
2020-11-29 11:39:41 +01:00
@client.event
async def on_message(message):
2021-08-17 11:35:25 +02:00
"""Triggered a message is received"""
2020-11-29 11:39:41 +01:00
await client.process_commands(message)
if message.author == client.user:
return
2021-08-22 15:50:27 +02:00
2021-05-31 20:26:57 +02:00
"""informations concernant le bot lorsqu'il est mentionner"""
2020-11-29 11:39:41 +01:00
if client.user.mention == message.content.replace("!",""):
ctx = await client.get_context(message)
prefix = await client.get_prefix(message)
await ctx.send(f">>> Coucou !\nMon préfix est `{prefix}` et ma commande d'aide est `{prefix}help`")
2021-08-22 15:47:40 +02:00
"""Ses évenements sont pour le reminder et le todo"""
@client.event
async def on_raw_reaction_add(payload):
"""Triggered when a reaction is added"""
message, embed = await listReaction(client, payload)
if message:
await message.edit(embed = embed)
@client.event
async def on_raw_reaction_remove(payload):
"""Triggered when a reaction is removed"""
message, embed = await listReaction(client, payload)
if message:
await message.edit(embed = embed)
2021-05-28 13:52:37 +02:00
print("Connexion à Discord...", end = " ")
2021-07-28 03:17:37 +02:00
client.run(keys["TOKEN_DISCORD"])