asyncdefon_message(message):#this will run on every message
print(str(message.author)+" sent: "+str(message.content)+", latency: "+str(bot.latency)+" ms, Bot: "+str(message.author.bot))#print some information about the message to the console
if(message.author.bot)or(message.author.id==bot.user.id):#check if the bot has written the message, if yes stop parsing it
res=c.execute("SELECT textXP FROM MESSAGES WHERE user=? AND server = ?",[message.author.id,message.guild.id])#try to get the current textXP of the user
c.execute("INSERT INTO MESSAGES VALUES (?, ?, ?, 0, 0)",[message.author.id,message.guild.id,messageXP])#if Bot can't find the user in the Database create a new entry
c.execute("UPDATE MESSAGES SET textXP = ? WHERE user = ? AND server = ?",[messages,message.author.id,message.guild.id])#update the textXP value in the Database
res=c.execute("SELECT reactionXP FROM MESSAGES WHERE user=? AND server = ?",[message.user_id,message.guild_id])#try to get the reactionXP from the database
c.execute("INSERT INTO MESSAGES VALUES (?, ?, 0, ?, 0)",[message.user_id,message.guild_id,reactionXP])#if bot can't find the database entry for the user create a new one
c.execute("UPDATE MESSAGES SET reactionXP = ? WHERE user = ? AND server = ?",[messages,message.user_id,message.guild_id])#update the reactionXP with the new values
polls[str(message.message_id)]# check if the reaction is on a poll
calls=c.execute("SELECT calls FROM poll WHERE pollID = ? AND reaction = ?",[message.message_id,str(message.emoji)]).fetchone()[0]# if yes update the database and add 1 vote
polls[str(message.message_id)]#check if the message is a poll
calls=c.execute("SELECT calls FROM poll WHERE pollID = ? AND reaction = ?",[message.message_id,str(message.emoji)]).fetchone()[0]#if yes update the database and remove 1 vote
result=c.execute("SELECT startTime FROM VcActive WHERE user=?",[member.id]).fetchone()[0]#if yes, get the join time from the database
c.execute("DELETE FROM VcActive WHERE user=?",[member.id])#delete the entrie from the voicechat in the database
xp=(time()-result)/minutesPerVcXP#calculate the xp
result=c.execute("SELECT vcXP FROM MESSAGES WHERE user=? AND server = ?",[member.id,member.guild.id]).fetchone()[0]#update the user database and set the new Voice XP
result=c.execute("SELECT startTime FROM VcActive WHERE user = ?",[member.id]).fetchone()[0]#get the join time from database
c.execute("DELETE FROM VcActive WHERE user=?",[member.id])# delete the join time entry
xp=(time()-result)/60/minutesPerVcXP#calculate the XP
result=c.execute("SELECT vcXP FROM MESSAGES WHERE user=? AND server = ?",[member.id,member.guild.id]).fetchone()[0]#update the user Table and set the new voiceXP
xp=c.execute("SELECT textXP, reactionXP, vcXP FROM MESSAGES WHERE user=? AND server = ?",[user.id,ctx.message.guild.id]).fetchone()#get the xp of the user from database
unsorted=c.execute("SELECT user, textXP, reactionXP, vcXP FROM MESSAGES WHERE server = ?",[ctx.message.guild.id]).fetchall()#get all users from the database (for the ranking)
ranking=[]#to this variable we will later add the users sorted
whilelen(unsorted)>0:#do this while we have entries in unsorted
lowest=unsorted[0]#just set the first entrie in lowest, so we can later overwrite it if we find a user with less XP
forentriesinunsorted:#go through every user, calculate the XP and check if its lower than the current lowest
unsorted=c.execute("SELECT user, textXP, reactionXP, vcXP FROM MESSAGES WHERE server = ?",[ctx.message.guild.id]).fetchall()#get all users from the database (for the ranking)
ranking=[]#to this variable we will later add the users sorted
whilelen(unsorted)>0:#do this while we have entries in unsorted
lowest=unsorted[0]#just set the first entrie in lowest, so we can later overwrite it if we find a user with less XP
forentriesinunsorted:#go through every user, calculate the XP and check if its lower than the current lowest
badges=[":first_place: ",":second_place: ",":third_place: ",":four: ",":five: ",":six: ",":seven: ",":eight: ",":nine: ",":keycap_ten: "]#the emoji in front of the username
message=""#in this variable we will prepare in the next step the response
forentriesinreversed(ranking):#loop through every user in reverse order to get the best first and andd his statistics to message
asyncdefping(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\nLatency:: "+str(round(bot.latency*1000))+"ms\nResponse :: "+str(delta)+"ms```")#make the response, we format it as code and select prolog as language for nice cloloring
@bot.command(pass_context=True,brief="starts a poll",description="starts a poll, please give as first argument the question (for sentences please use \") and then the posibilities (leave empty for yes or no)")
asyncdefresult(ctx,id):#this function prints the result of a poll
poll_message=polls[str(id)]#get the poll message from the variable
embed=poll_message.embeds[0]#get the embed (the content)
delpolls[str(id)]#delte the poll from the poll message array
unformatted_options=[x.strip()forxinpoll_message.embeds[0].description.split('\n')]#split the poll options (split every line and remove all leading and trailing spaces)
opt_dict={}
foroptionsinunformatted_options:#for every option: split on the space between emoji and text and put it in a dict, with the emoji as key and the emoji + the text as value
options=options.split("",1)
opt_dict[options[0]]=options[0]+""+options[1]
request=c.execute("SELECT calls, reaction FROM poll WHERE pollID = ?",[id]).fetchall()#get the votes from the database
description=""
forresultsinrequest:#loop through the entries of the database and make the result
embed2=discord.Embed(title="",description="You can find the result of the poll in the original message [here]("+poll_message.jump_url+").")#This message will be printed to the end of the chat with a link to the original (where the results are)
awaitctx.message.channel.send(embed=embed2)#print the result message from one line up