2020-10-14 02:50:24 +02:00
import discord
from discord . ext import commands
2020-11-24 09:20:09 +01:00
from random import randint , choice , shuffle
2020-10-14 02:50:24 +02:00
import time
2020-11-10 19:04:01 +01:00
from datetime import datetime
from pytz import timezone
2020-11-24 09:20:09 +01:00
import re
2020-10-14 02:50:24 +02:00
2020-11-09 09:58:01 +01:00
def setup ( bot ) :
bot . add_cog ( Commands ( bot ) )
2020-10-14 02:50:24 +02:00
class Commands ( commands . Cog ) :
2020-10-14 03:02:43 +02:00
""" Commandes générales du bot. """
2020-10-14 02:50:24 +02:00
2020-10-14 03:02:43 +02:00
def __init__ ( self , bot ) :
self . bot = bot
2020-10-14 02:50:24 +02:00
2020-10-14 03:23:14 +02:00
# dans le message envoyer : le premier chiffre est la latence du protocole Discord WebSocket,
# le deuxieme c'est le temps que le bot a pris pour faire les p'tits calculs,
# le troisième c'est le temps que le bot a pris pour réagir au message
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' ping ' )
async def _ping ( self , ctx , * , question = ' 0 ' ) :
2020-10-14 03:02:43 +02:00
""" Affiche mon ping. \n ➡ Syntaxe: .ping [help] """
if question == ' help ' :
2020-10-20 15:06:08 +02:00
return await ctx . send ( embed = discord . Embed ( color = randint ( 0 , 0xFFFFFF ) ,
description = " :hourglass: correspond au temps entre deux battements de cœurs (en millisecondes) \n \n "
+ " :stopwatch: correspond au temps que met le bot a calculer le ping (en millisecondes) \n \n "
+ " :heartbeat: correspond au temps que met le bot a réagir au messages (en millisecondes) " ) )
2020-10-14 03:02:43 +02:00
else :
now = int ( round ( time . time ( ) * 1000 ) )
ping = now - int ( round ( ctx . message . created_at . timestamp ( ) * 1000 ) )
embed = discord . Embed ( description = ' Pinging... ' )
message = await ctx . send ( embed = embed )
ping2 = int ( round ( time . time ( ) * 1000 ) ) - now
2020-10-20 15:06:08 +02:00
await message . edit ( embed = discord . Embed ( color = randint ( 0 , 0xFFFFFF ) ,
description = f ' :hourglass: { round ( self . bot . latency * 1000 ) } ms \n \n :stopwatch: { ping2 } ms \n \n :heartbeat: { ping } ms ' ) )
2020-10-14 03:02:43 +02:00
await ctx . message . add_reaction ( emoji = ' ✅ ' )
2020-10-14 02:50:24 +02:00
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' calc ' , aliases = [ ' calculatrice ' , ' calcu ' ' calcul ' ] )
async def _calc ( self , ctx , * , msg ) :
2020-11-10 19:34:02 +01:00
""" Calculatrice. \n ➡ Syntaxe: .calc/calculatrice/calcu/calcul <calcul> """
2020-10-14 03:02:43 +02:00
equation = msg . replace ( ' ^ ' , ' ** ' ) . replace ( ' x ' , ' * ' ) . replace ( ' × ' , ' * ' ) . replace ( ' ÷ ' , ' / ' ) . replace ( ' ≥ ' , ' >= ' ) . replace ( ' ≤ ' , ' <= ' )
try :
try :
if ' = ' in equation :
if ' < ' in equation :
left = eval ( equation . split ( " <= " ) [ 0 ] )
right = eval ( equation . split ( " <= " ) [ 1 ] )
answer = str ( left < = right )
elif ' > ' in equation :
left = eval ( equation . split ( " >= " ) [ 0 ] )
right = eval ( equation . split ( " >= " ) [ 1 ] )
answer = str ( left > = right )
else :
left = eval ( equation . split ( " = " ) [ 0 ] )
right = eval ( equation . split ( " = " ) [ 1 ] )
answer = str ( left == right )
else :
answer = str ( eval ( equation ) )
except ZeroDivisionError :
return await ctx . send ( " Tu ne peux pas divisé par 0. " )
except TypeError :
return await ctx . send ( " Requête de calcul invalide. " )
if ' . ' in answer :
aftercomma = answer . split ( " . " ) [ 1 ]
if len ( aftercomma ) > 2 :
answer = str ( round ( float ( answer ) , 2 ) )
equation = f " ' { equation } ' arrondi à 2 "
equation = equation . replace ( ' * ' , ' × ' ) . replace ( ' / ' , ' ÷ ' ) . replace ( ' >= ' , ' ≥ ' ) . replace ( ' <= ' , ' ≤ ' )
embed = discord . Embed ( color = randint ( 0 , 0xFFFFFF ) , title = ' Calculatrice ' )
embed . set_footer ( text = ctx . author )
2020-10-14 02:50:24 +02:00
2020-10-14 03:02:43 +02:00
embed . add_field ( name = ' Calcul : ' , value = equation , inline = False )
embed . add_field ( name = ' Réponse : ' , value = answer . replace ( ' False ' , ' Faux ' ) . replace ( ' True ' , ' Vrai ' ) , inline = False )
await ctx . message . add_reaction ( emoji = ' ✅ ' )
await ctx . send ( content = None , embed = embed )
2020-11-24 09:23:24 +01:00
@_calc.error
2020-11-24 14:17:44 +01:00
async def _calc_error ( self , ctx , error ) :
2020-10-14 03:02:43 +02:00
await ctx . send ( " Tu n ' as pas spécifié de calcul. " )
2020-10-14 02:50:24 +02:00
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' syntax ' , aliases = [ ' syntaxe ' ] )
async def _syntax ( self , ctx ) :
2020-11-10 19:34:02 +01:00
""" Informations pour bien éditer son texte. \n ➡ Syntaxe: .syntax/syntaxe """
2020-10-14 03:02:43 +02:00
syntaxe = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " ```Js \n " )
syntaxe + = discord . utils . escape_markdown ( " //code en js (possible de remplacer ' js ' par d ' autres languages . adaptez le !) \n " )
syntaxe + = discord . utils . escape_markdown ( ' console.log( " hi " ); \n ' )
syntaxe + = discord . utils . escape_markdown ( " ``` \n " )
syntaxe + = " ```Js \n "
syntaxe + = " //code en js (possible de remplacer ' js ' par d ' autres languages . adaptez le !) \n "
syntaxe + = ' console.log( " hi " ); \n '
syntaxe + = " ``` \n "
2020-11-09 09:47:17 +01:00
syntaxe + = " Si ton code est trop long, mets le sur <https://pastebin.com/> \n "
2020-10-14 03:02:43 +02:00
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " `code sur une seule ligne` \n " )
syntaxe + = " `code sur une seule ligne` \n "
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " *texte en italique* \n " )
syntaxe + = " *texte en italique* \n "
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " **text en gras** \n " )
syntaxe + = " **text en gras** \n "
syntaxe + = " ----------------------------------------------------- \n "
2020-11-10 19:06:57 +01:00
syntaxe + = discord . utils . escape_markdown ( " <<https://www.youtube.com/watch?v=GhuYKL5NUYg>> \n " )
2020-11-10 19:11:25 +01:00
syntaxe + = " Un lien entre crochet, ça empêche Discord de rajouté son intégration automatique (mais le lien fonctionnera toujours). \n "
2020-11-10 19:04:44 +01:00
syntaxe + = " ----------------------------------------------------- \n "
2020-11-09 09:47:17 +01:00
syntaxe + = discord . utils . escape_markdown ( " __texte souligné__ \n " )
syntaxe + = " __texte souligné__ \n "
2020-10-14 03:02:43 +02:00
syntaxe + = " ----------------------------------------------------- \n "
2020-11-09 09:47:17 +01:00
syntaxe + = discord . utils . escape_markdown ( " ~~texte barré~~ \n " )
syntaxe + = " ~~texte barré~~ \n "
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " ~~__***text en italique-gras-souligné-barré***__~~ \n " )
syntaxe + = " ~~__***text en italique-gras-souligné-barré***__~~ \n "
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " \ :joy: <- l ' emoji ne va pas fonctionné grâce au \ \n " )
syntaxe + = " \ :joy: <- l ' emoji ne va pas fonctionné grâce au \ \n "
2020-10-15 10:12:25 +02:00
syntaxe + = " ----------------------------------------------------- \n "
2020-10-14 03:02:43 +02:00
syntaxe + = discord . utils . escape_markdown ( " > cette ligne est cité \n pas celle là \n " )
syntaxe + = " > cette ligne est cité \n pas celle là \n "
syntaxe + = " ----------------------------------------------------- \n "
syntaxe + = discord . utils . escape_markdown ( " >>> cette ligne est cité \n celle là aussi (et elles le seront toutes!) \n " )
syntaxe + = " >>> cette ligne est cité \n celle là aussi (et elles le seront toutes!) \n "
await ctx . message . add_reaction ( emoji = ' ✅ ' )
await ctx . send ( syntaxe )
2020-10-14 02:50:24 +02:00
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' infos ' , aliases = [ ' info ' ] )
async def _infos ( self , ctx ) :
2020-11-09 10:07:03 +01:00
""" Donne des infos sur le bot. \n ➡ Syntaxe: .infos/info """
2020-11-09 09:52:00 +01:00
appinfo = await self . bot . application_info ( )
2020-10-14 02:50:24 +02:00
2020-11-09 09:52:00 +01:00
embed = discord . Embed ( color = randint ( 0 , 0xFFFFFF ) )
embed . set_author ( name = appinfo . name , icon_url = self . bot . user . avatar_url )
2020-10-14 02:50:24 +02:00
2020-10-20 15:06:08 +02:00
# liste utilisateurs de tous les serveurs où le bot est, en ligne
total_online = len ( { m . id for m in self . bot . get_all_members ( ) if m . status is discord . Status . online } )
2020-10-14 03:23:14 +02:00
total_unique = len ( self . bot . users ) # pareil mais en comptants les hors lignes aussi
2020-10-14 02:50:24 +02:00
2020-10-14 03:23:14 +02:00
# liste des différents canaux
2020-10-14 03:02:43 +02:00
voice_channels = [ ]
text_channels = [ ]
for guild in self . bot . guilds :
voice_channels . extend ( guild . voice_channels )
text_channels . extend ( guild . text_channels )
2020-10-14 02:50:24 +02:00
2020-10-14 03:02:43 +02:00
text = len ( text_channels )
voice = len ( voice_channels )
2020-10-14 02:50:24 +02:00
2020-11-09 09:52:00 +01:00
embed . add_field ( name = ' Dev ' , value = f " [ { appinfo . owner } ](https://github.com/Mylloon) " ) # pub gratuite
2020-10-14 03:02:43 +02:00
embed . add_field ( name = ' Serveurs ' , value = len ( self . bot . guilds ) )
embed . add_field ( name = ' Membres ' , value = f ' { total_unique } au total \n { total_online } en ligne ' )
embed . add_field ( name = ' Channels ' , value = f ' { text } textuelles \n { voice } vocales ' )
2020-10-20 15:06:08 +02:00
# ca peut être utile de connaitre quel version le bot utilise sans devoir se connecter a distance au serveur qui fait tourner le bot
embed . set_footer ( text = f ' Basé sur discord.py { discord . __version__ } ' )
2020-11-17 13:16:05 +01:00
await ctx . message . add_reaction ( emoji = ' ✅ ' )
2020-11-10 19:04:01 +01:00
await ctx . send ( embed = embed )
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' whois ' )
async def _whois ( self , ctx , * user : discord . Member ) :
2020-11-10 19:04:01 +01:00
""" Affiche les infos sur l ' utilisateur. \n ➡ Syntaxe: .whois [user] """
if len ( user ) < = 1 :
if user == ( ) :
user = [ ctx . author ]
nom = f " { user [ 0 ] . name } # { user [ 0 ] . discriminator } "
if user [ 0 ] . nick :
nom = f " { user [ 0 ] . nick } ( { user [ 0 ] . name } # { user [ 0 ] . discriminator } ) "
embed = discord . Embed ( color = randint ( 0 , 0xFFFFFF ) ) . set_author ( name = nom , icon_url = user [ 0 ] . avatar_url )
embed . add_field ( name = " ID " , value = user [ 0 ] . id )
value = str ( user [ 0 ] . created_at . astimezone ( timezone ( ' Europe/Paris ' ) ) ) [ : - 13 ] . replace ( ' - ' , ' / ' ) . split ( )
embed . add_field ( name = " Compte créé le " , value = f " { value [ 0 ] [ 8 : ] } / { value [ 0 ] [ 5 : - 3 ] } / { value [ 0 ] [ : 4 ] } à { value [ 1 ] } " )
2020-11-24 14:17:44 +01:00
embed . add_field ( name = " Âge du compte " , value = self . _age_layout ( self . _get_age ( user [ 0 ] . created_at ) ) )
2020-11-10 19:04:01 +01:00
embed . add_field ( name = " Mention " , value = user [ 0 ] . mention )
value = str ( user [ 0 ] . joined_at . astimezone ( timezone ( ' Europe/Paris ' ) ) ) [ : - 13 ] . replace ( ' - ' , ' / ' ) . split ( )
embed . add_field ( name = " Serveur rejoint le " , value = f " { value [ 0 ] [ 8 : ] } / { value [ 0 ] [ 5 : - 3 ] } / { value [ 0 ] [ : 4 ] } à { value [ 1 ] } " )
2020-11-24 14:17:44 +01:00
embed . add_field ( name = " Est sur le serveur depuis " , value = self . _age_layout ( self . _get_age ( user [ 0 ] . joined_at ) ) )
2020-11-17 13:16:05 +01:00
await ctx . message . add_reaction ( emoji = ' ✅ ' )
2020-11-10 19:04:01 +01:00
return await ctx . send ( embed = embed )
await ctx . send ( " Tu mentionnes trop d ' utilisateurs : `.whois [@Membre]` " )
2020-11-24 14:17:44 +01:00
def _get_age ( self , date ) :
2020-11-10 19:04:01 +01:00
joursRestants = datetime . now ( ) - date
years = joursRestants . total_seconds ( ) / ( 365.242 * 24 * 3600 )
months = ( years - int ( years ) ) * 12
days = ( months - int ( months ) ) * ( 365.242 / 12 )
hours = ( days - int ( days ) ) * 24
minutes = ( hours - int ( hours ) ) * 60
seconds = ( minutes - int ( minutes ) ) * 60
return ( int ( years ) , int ( months ) , int ( days ) , int ( hours ) , int ( minutes ) , int ( seconds ) )
2020-11-24 14:17:44 +01:00
def _age_layout ( self , tuple ) :
2020-11-10 19:04:01 +01:00
time = { }
time [ 0 ] , time [ 1 ] , time [ 2 ] , time [ 3 ] , time [ 4 ] , time [ 5 ] = " an " , " mois " , " jour " , " heure " , " minute " , " seconde "
for i in range ( len ( tuple ) ) :
if tuple [ i ] > 1 and i != 1 :
time [ i ] = time [ i ] + " s "
message = " "
if tuple [ 5 ] > 0 : # pour les secondes
affichage = [ 5 ] # on affiche que : seconde
if tuple [ 4 ] > 0 :
affichage = [ 4 , 5 ] # on affiche : minute + seconde
if tuple [ 3 ] > 0 :
affichage = [ 3 , 4 , 5 ] # on affiche : heure + minute + seconde
if tuple [ 2 ] > 0 :
affichage = [ 2 , 3 , 4 ] # on affiche : jour + heure + minute
if tuple [ 1 ] > 0 :
affichage = [ 1 , 2 , 3 ] # on affiche : mois + jour + heure
if tuple [ 0 ] > 0 :
affichage = [ 0 , 1 , 3 ] # on affiche : an + mois + heure
for i in affichage :
message = message + f " , { tuple [ i ] } { time [ i ] } "
2020-11-17 15:07:31 +01:00
return message [ 2 : ]
2020-11-24 09:22:24 +01:00
@commands.command ( name = ' appel ' )
2020-11-17 15:07:31 +01:00
@commands.has_any_role ( " Professeur " , " professeur " , " Prof " , " prof " )
2020-11-24 09:22:24 +01:00
async def _appel ( self , ctx , * , voice_channel : int = None ) :
2020-11-24 12:45:59 +01:00
""" Fais l ' appel. \n ➡ Syntaxe: .appel [ID salon vocal] """
2020-11-17 15:07:31 +01:00
voice_channels = [ ]
voice_channels . extend ( ctx . guild . voice_channels )
2020-11-17 15:08:46 +01:00
await ctx . message . add_reaction ( emoji = " ✅ " )
2020-11-24 12:39:10 +01:00
if voice_channel :
canal = self . bot . get_channel ( voice_channel )
if canal . type . __str__ ( ) == " voice " :
voice_channels = [ canal ]
else :
return await ctx . send ( " Tu as spécifié un channel textuelle et non vocal. " )
2020-11-17 15:07:31 +01:00
if len ( voice_channels ) > 0 :
2020-11-17 15:08:46 +01:00
embed = discord . Embed ( title = " Réagissez à ce message avec ✋ pour signaler votre présence. " , description = f " (attention, je réagis aussi) — Professeur : { ctx . author . mention } " )
2020-11-17 15:07:31 +01:00
for channel in voice_channels :
prof = [ ]
for role in [ " Professeur " , " professeur " , " Prof " , " prof " ] :
role = discord . utils . get ( ctx . guild . roles , name = role )
for user in channel . members :
if role in user . roles and user not in prof :
prof . append ( user )
eleve = channel . members
for user in channel . members :
if user in prof :
eleve . remove ( user )
value = f " ** { len ( channel . members ) } ** personne { ' s ' if len ( channel . members ) > 1 else ' ' } connectée { ' s ' if len ( channel . members ) > 1 else ' ' } . \n Dont { len ( eleve ) } élève { ' s ' if len ( eleve ) > 1 else ' ' } et { len ( prof ) } professeur { ' s ' if len ( prof ) > 1 else ' ' } . "
embed . add_field ( name = f " 🔊 { channel . name } " , value = value , inline = False )
message = await ctx . send ( embed = embed )
else :
message = await ctx . send ( " Aucun salon vocal dans ce serveur, réagissez à ce message avec ✋ pour signaler votre présence (attention, je réagis aussi). " )
2020-11-17 15:08:46 +01:00
await message . add_reaction ( emoji = " ✋ " )
2020-11-24 09:23:24 +01:00
@_appel.error
2020-11-24 14:17:44 +01:00
async def _appel_error ( self , ctx , error ) :
2020-11-17 15:07:31 +01:00
if isinstance ( error , commands . CheckFailure ) :
await ctx . send ( " Tu n ' as pas la permission de faire cette commande, demande à un professeur. " )
else :
2020-11-24 09:20:09 +01:00
await ctx . send ( " Une erreur est survenue, syntaxe: `.appel [ID salon vocal]`. " )
@commands.command ( name = ' sondage ' )
async def _sondage ( self , ctx , * args ) :
""" Fais un sondage. \n ➡ Syntaxe: .sondage " <Question> " " <Proposition1> " " <Proposition...> " " <Proposition20> " """
args = list ( args )
if len ( args ) > 2 :
question = args [ 0 ] . replace ( " <@! " , " " ) . replace ( " > " , " " ) . replace ( " <@ " , " " )
for i in re . findall ( r ' \ d+ ' , question ) :
2020-11-24 14:17:44 +01:00
ii = self . _user_or_nick ( ctx . author . guild . get_member ( int ( i ) ) )
2020-11-24 09:20:09 +01:00
try :
question = question . replace ( i , ii )
except :
pass
propositions = args [ 1 : ]
if len ( propositions ) < = 20 :
message = " "
emojis = { }
emojis [ 0 ] = [ ' 1️ ⃣ ' , ' 2️ ⃣ ' , ' 3️ ⃣ ' , ' 4️ ⃣ ' , ' 5️ ⃣ ' , ' 6️ ⃣ ' , ' 7️ ⃣ ' , ' 8️ ⃣ ' , ' 9️ ⃣ ' , ' 🔟 ' ]
emojis [ 1 ] = [
' 🟤 ' , ' 🔴 ' , ' 🟠 ' , ' 🟡 ' , ' 🟢 ' , ' 🔵 ' , ' 🟣 ' , ' 🔘 ' , ' ❤ ' , ' 💜 ' ,
' 🟫 ' , ' 🟥 ' , ' 🟧 ' , ' 🟨 ' , ' 🟩 ' , ' 🟦 ' , ' 🟪 ' , ' 🔳 ' , ' 🧡 ' , ' 💙 '
]
mixable = True
if len ( propositions ) < = 10 :
emojis_chosen = emojis [ randint ( 0 , len ( emojis ) - 1 ) ]
emojis_chosen = emojis_chosen [ : 10 ]
if len ( propositions ) < = 8 :
emojis_chosen = emojis_chosen [ : 8 ]
else :
emojis_chosen = emojis [ randint ( 1 , len ( emojis ) - 1 ) ]
if emojis [ 0 ] [ 0 ] in emojis_chosen : # rajouter ici les listes qui ne doivent pas être mélanger
mixable = False
if mixable :
shuffle ( emojis_chosen )
for i in range ( len ( args [ 1 : ] ) ) :
message + = f " { emojis_chosen [ i ] } -> { propositions [ i ] } \n "
2020-11-24 14:17:44 +01:00
embed = discord . Embed ( title = question , description = message , color = randint ( 0 , 0xFFFFFF ) ) . set_footer ( text = self . _user_or_nick ( ctx . author ) , icon_url = ctx . author . avatar_url )
2020-11-24 09:20:09 +01:00
sondage = await ctx . send ( embed = embed )
for i in range ( len ( args [ 1 : ] ) ) :
await sondage . add_reaction ( emoji = emojis_chosen [ i ] )
return await ctx . message . add_reaction ( emoji = ' ✅ ' )
else :
return await ctx . send ( f " Désolé, mais tu as mis trop de possibilités (maximum : 20) " )
else :
return await ctx . send ( f ' Désolé, mais il manque des arguments : `.sondage " <Question> " " <Proposition1> " " <Proposition...> " " <Proposition20> " ` ' )
2020-11-24 14:17:44 +01:00
def _user_or_nick ( self , user ) :
2020-11-24 09:20:09 +01:00
if user . nick :
return f " { user . nick } ( { user . name } # { user . discriminator } ) "
else :
2020-11-24 14:19:31 +01:00
return f " { user . name } # { user . discriminator } "