2020-07-27 13:17:05 +02:00
from config import *
import discord
from discord . ext import commands
from datetime import datetime
2020-07-27 14:43:44 +02:00
import pymysql
from requests import get
from json import loads
2020-07-27 13:17:05 +02:00
bot = commands . Bot ( command_prefix = prefix )
@bot.event #print the username and id to the console
async def on_ready ( ) :
print ( ' Logged in as ' )
print ( bot . user . name )
print ( bot . user . id )
print ( ' ------ ' )
2020-07-27 14:43:44 +02:00
@bot.event
async def on_member_update ( before , after ) :
roles = [ ]
for role in after . roles : #get the name of all roles except @everyone
if ( role . name != " @everyone " ) :
roles . append ( role . name )
for role in ignoreRoles : #check if the user has one of the ignore roles, if yes stop here
if role in roles :
return
rolesString = " "
for role in roles : #try to find the name for the role in the role map and convert it. After that add it to roleString
try :
rolesString = rolesString + roleMap [ role ] + " , "
except :
pass
rolesString = rolesString [ : - 1 ] #remove the "," at the end
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
cursor . execute ( " SELECT * FROM users WHERE discord= %s " , ( after . id ) )
if ( len ( cursor . fetchall ( ) ) == 0 ) :
cursor . execute ( " INSERT INTO users (discord, roles) VALUES ( %s , %s ) " , ( after . id , rolesString ) )
else :
cursor . execute ( " UPDATE users SET roles= %s WHERE discord= %s " , ( rolesString , after . id ) )
db . commit ( )
db . close ( )
print ( " updated profile for " + after . name )
@bot.event
async def on_member_join ( member ) :
r = get ( " https://data.tilera.xyz/api/acapi/discord.php?id= " + str ( member . id ) )
response = loads ( r . text )
try :
ytchannel = response [ " ytchannel " ]
except :
pass
try :
twchannel = response [ " twchannel " ]
except :
pass
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
cursor . execute ( " INSERT INTO users (discord, ytchannel, twchannel) VALUES ( %s , %s , %s ) " , ( member . id , ytchannel , twchannel ) )
db . commit ( )
db . close ( )
print ( member . name + " joined the Server " )
@bot.event
async def on_member_remove ( member ) :
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
cursor . execute ( " DELETE FROM users WHERE discord= %s " , ( member . id ) )
db . commit ( )
db . close ( )
print ( member . name + " left the server " )
2020-07-27 14:50:36 +02:00
@bot.command ( pass_context = True , brief = " set your minecraft username " )
async def mc ( ctx , minecraftUsername : str ) :
r = get ( " https://data.tilera.xyz/api/acapi/mcuuid.php?id= " + minecraftUsername )
respone = loads ( r . text )
if ( respone [ " status " ] == " 404 " ) :
await ctx . send ( " Can ' t find your minecraft username, please check your spelling and try again. " )
return
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
2020-07-27 16:00:16 +02:00
cursor . execute ( " SELECT * FROM users WHERE discord= %s " , ( ctx . author . id ) )
if ( len ( cursor . fetchall ( ) ) == 0 ) :
2020-07-27 16:16:16 +02:00
cursor . execute ( " INSERT INTO users (discord, mcuuid) VALUES ( %s , %s ) " , ( ctx . author . id , respone [ " uuid " ] ) )
2020-07-27 16:00:16 +02:00
else :
cursor . execute ( " UPDATE users SET mcuuid= %s WHERE discord= %s " , ( respone [ " uuid " ] , ctx . author . id ) )
2020-07-27 14:50:36 +02:00
db . commit ( )
db . close ( )
await ctx . send ( " successfully updated your minecraft username " )
print ( ctx . author . name + " updated his Minecraft username " )
2020-07-27 14:53:08 +02:00
@bot.command ( pass_context = True , brief = " set your YouTube channel URL " )
async def yt ( ctx , youTubeChannelURL ) :
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
2020-07-27 16:00:16 +02:00
cursor . execute ( " SELECT * FROM users WHERE discord= %s " , ( ctx . author . id ) )
if ( len ( cursor . fetchall ( ) ) == 0 ) :
2020-07-27 16:16:16 +02:00
cursor . execute ( " INSERT INTO users (discord, ytchannel) VALUES ( %s , %s ) " , ( ctx . author . id , youTubeChannelURL ) )
2020-07-27 16:00:16 +02:00
else :
cursor . execute ( " UPDATE users SET ytchannel= %s WHERE discord= %s " , ( youTubeChannelURL , ctx . author . id ) )
2020-07-27 14:53:08 +02:00
db . commit ( )
db . close ( )
await ctx . send ( " successfully updated your YouTube channel URL " )
print ( ctx . author . name + " updated his youtube channel URL " )
@bot.command ( pass_context = True , brief = " set your Twitch channel URL " )
async def twitch ( ctx , twitchChannelURL ) :
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
2020-07-27 16:00:16 +02:00
cursor . execute ( " SELECT * FROM users WHERE discord= %s " , ( ctx . author . id ) )
if ( len ( cursor . fetchall ( ) ) == 0 ) :
2020-07-27 16:16:16 +02:00
cursor . execute ( " INSERT INTO users (discord, twchannel) VALUES ( %s , %s ) " , ( ctx . author . id , twitchChannelURL ) )
2020-07-27 16:00:16 +02:00
else :
cursor . execute ( " UPDATE users SET twchannel= %s WHERE discord= %s " , ( twitchChannelURL , ctx . author . id ) )
2020-07-27 14:53:08 +02:00
db . commit ( )
db . close ( )
await ctx . send ( " successfully updated your Twitch channel URL " )
print ( ctx . author . name + " updated his twitch channel URL " )
2020-07-27 16:09:27 +02:00
@bot.command ( pass_context = True , brief = " updates the roles in the database of a user " )
async def update ( ctx , user : discord . Member = None ) :
if ( user == None ) : #check if the message author submitted a different user to check, if not use the message author
user = ctx . message . author
roles = [ ]
for role in user . roles : #get the name of all roles except @everyone
if ( role . name != " @everyone " ) :
roles . append ( role . name )
for role in ignoreRoles : #check if the user has one of the ignore roles, if yes stop here
if role in roles :
return
rolesString = " "
for role in roles : #try to find the name for the role in the role map and convert it. After that add it to roleString
try :
rolesString = rolesString + roleMap [ role ] + " , "
except :
pass
rolesString = rolesString [ : - 1 ] #remove the "," at the end
db = pymysql . connect ( sqlServer , sqlUser , sqlPassword , sqlDatabase )
cursor = db . cursor ( )
cursor . execute ( " SELECT * FROM users WHERE discord= %s " , ( user . id ) )
if ( len ( cursor . fetchall ( ) ) == 0 ) :
cursor . execute ( " INSERT INTO users (discord, roles) VALUES ( %s , %s ) " , ( user . id , rolesString ) )
else :
cursor . execute ( " UPDATE users SET roles= %s WHERE discord= %s " , ( rolesString , user . id ) )
db . commit ( )
db . close ( )
await ctx . send ( " profile for user " + user . name + " updated " )
print ( " updated profile for " + user . name )
2020-07-27 13:17:05 +02:00
@bot.command ( pass_context = True , brief = " prints the ping time of the bot " )
async def ping ( ctx ) : #prints the ping and the time the bot needed to process this command
now = datetime . utcnow ( ) #get the current time
delta = round ( ( now . microsecond - ctx . message . created_at . microsecond ) / 1000 ) #substract the time the message was created from the current time 8in microsecconds), convert this to millisecconds and round
embed = discord . Embed ( title = " :ping_pong: | Pong! " , description = " ```prolog \n Latency :: " + str ( round ( bot . latency * 1000 ) ) + " ms \n Response :: " + str ( delta ) + " ms``` " ) #make the response, we format it as code and select prolog as language for nice cloloring
await ctx . message . channel . send ( embed = embed ) #send the prepared message
bot . run ( token ) #start the bot with the token in config.py