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

135 lines
6.6 KiB
Python
Raw 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
import re
import os
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-05-07 16:44:47 +02:00
customPrefix = os.environ['PREFIX']
customTimezone = os.environ['TIMEZONE']
from utils.core import userOrNick, goodTimezone
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.load_extension("cogs.help")
client.load_extension("cogs.utils")
client.load_extension("cogs.internet")
client.load_extension("cogs.music")
client.load_extension("cogs.games")
client.load_extension("cogs.fun")
client.load_extension("cogs.autopublish")
2021-05-15 20:07:49 +02:00
client.load_extension("cogs.school")
client.load_extension("cogs.confreriedukassoulait")
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-05-28 13:52:37 +02:00
print(f"Connecté !")
2020-11-29 11:39:41 +01:00
@client.event
async def on_ready():
await client.change_presence(status = discord.Status.online, activity = discord.Activity(name = f"{customPrefix}help", type = discord.ActivityType.playing))
2020-11-29 11:39:41 +01:00
print("Bot prêt.")
@client.event
async def on_command_error(ctx, error):
if not ctx.invoked_with.startswith(customPrefix):
2020-11-29 11:39:41 +01:00
print(error)
await ctx.message.add_reaction(emoji = '')
2020-11-29 11:39:41 +01:00
@client.event
async def on_message(message):
await client.process_commands(message)
if message.author == client.user:
return
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`")
urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message.content)
httpsString = "https://"
channelsString = "discord.com/channels/"
2020-11-29 11:39:41 +01:00
for i in range(len(urls)):
if urls[i].startswith(f"{httpsString}{channelsString}") or urls[i].startswith(f"{httpsString}ptb.{channelsString}") or urls[i].startswith(f"{httpsString}canary.{channelsString}"):
2020-11-29 11:39:41 +01:00
link = urls[i]
linkURL = link
if link.startswith(f"{httpsString}{channelsString}"):
2020-11-29 11:39:41 +01:00
link = f'000{link}'
if link.startswith(f"{httpsString}ptb.{channelsString}"):
link = link[1:]
if link.startswith(f"{httpsString}canary.{channelsString}"):
2020-11-29 11:39:41 +01:00
link = link[4:]
if "@me" in urls[i]:
return await message.channel.send("Je ne cite pas les messages privés.", delete_after = 5)
try:
if int(link[32:-38]) == message.guild.id:
msgID = await client.get_channel(int(link[51:-19])).fetch_message(int(link[70:]))
2020-12-11 12:29:28 +01:00
couleur = 0x2f3136
2021-02-22 05:25:04 +01:00
msgFiles = msgID.attachments
imageExtensions = ["jpg", "jpeg", "png", "webp", "gif"]
2021-02-22 05:30:41 +01:00
desc = msgID.content
2021-02-22 05:25:04 +01:00
if len(msgFiles) > 1:
listOfFiles = ""
for i in range(0, len(msgFiles)):
listOfFiles = f"{listOfFiles}, {msgFiles[i].filename}"
listOfFiles = listOfFiles[2:]
if len(msgID.content) > 0:
desc = f"{msgID.content}\n\nIl y a plusieurs fichiers dans ce message : {listOfFiles}"
else:
desc = f"Il y a plusieurs fichiers dans ce message : {listOfFiles}"
2020-11-29 11:39:41 +01:00
else:
2021-02-22 05:25:04 +01:00
if len(msgFiles) == 1:
if msgFiles[0].filename[-4:].split('.')[1] in imageExtensions:
2021-02-22 05:30:41 +01:00
if not len(msgID.content) > 0:
2021-02-22 05:25:04 +01:00
desc = f"Une image jointe : {msgFiles[0].filename}"
else:
linkFile = msgFiles[0].url
2021-02-22 05:30:41 +01:00
if not len(msgID.content) > 0:
2021-02-22 05:25:04 +01:00
desc = f"Un fichier joint : {msgFiles[0].filename}"
embed = discord.Embed(description = desc, colour = couleur)
2020-12-11 12:29:28 +01:00
auteur = "Auteur"
if message.author == msgID.author:
auteur = "Auteur & Citateur"
embed.add_field(name = auteur, value = msgID.author.mention, inline=True)
2021-02-22 05:25:04 +01:00
try:
if len(msgFiles) == 1:
if msgFiles[0].filename[-4:].split('.')[1] in imageExtensions:
embed.set_image(url=msgFiles[0].url)
else:
embed.add_field(name = "Fichier", value = f"[Lien]({linkFile})", inline=True)
except:
pass
2020-12-23 00:47:29 +01:00
embed.add_field(name = "Message", value = f"{msgID.channel.mention} - [Lien Message]({linkURL})", inline=True)
2020-11-29 11:39:41 +01:00
embed.set_author(name = "Citation", icon_url = msgID.author.avatar_url)
icon_url = message.author.avatar_url
date_1 = goodTimezone(msgID.created_at, 0, customTimezone)
2020-11-29 11:39:41 +01:00
edit = ""
if msgID.edited_at:
date_edit = goodTimezone(msgID.edited_at, 0, customTimezone)
2021-05-13 09:44:17 +02:00
edit = f" et modifié le {date_edit[0][8:]}/{date_edit[0][5:-3]}/{date_edit[0][:4]} à {date_edit[1]}"
2021-03-27 00:02:01 +01:00
messageDuBas = f"Posté le {date_1[0][8:]}/{date_1[0][5:-3]}/{date_1[0][:4]} à {date_1[1]}{edit}"
2020-11-29 11:39:41 +01:00
date_2 = goodTimezone(message.created_at, 0, customTimezone)
2020-11-29 11:39:41 +01:00
date_2 = f"{date_2[0][8:]}/{date_2[0][5:-3]}/{date_2[0][:4]} à {date_2[1]}"
2020-12-11 12:29:28 +01:00
if auteur == "Auteur":
messageDuBas = messageDuBas + f"\nCité par {userOrNick(message.author)} le {date_2}"
2021-03-27 00:02:01 +01:00
embed.set_footer(icon_url = icon_url, text = messageDuBas)
2020-11-29 11:39:41 +01:00
if message.content == linkURL.replace(' ',''):
await message.channel.send(embed = embed)
2020-11-29 11:39:41 +01:00
await message.delete()
else:
2021-05-28 13:29:52 +02:00
await message.reply(embed = embed, mention_author = False)
2020-11-29 11:39:41 +01:00
except Exception as e:
e = str(e)
if not "invalid literal for int() with base 10:" in e or not "404 Not Found (error code: 10008)" in e: # faute de frappe / message supprimé
print(e)
2021-05-28 13:52:37 +02:00
print("Connexion à Discord...", end = " ")
client.run(os.environ['TOKEN_DISCORD'])