1
0
Fork 0
mirror of https://gitlab.jonasled.de/jonasled/discordbot synced 2024-10-21 16:59:16 +02:00

renamed some database variables

This commit is contained in:
Jonas Leder 2020-04-02 20:51:49 +02:00
parent 16f697039a
commit 8adc06721e

78
bot.py
View file

@ -12,16 +12,16 @@ polls = {}
bot = commands.Bot(command_prefix=prefix) bot = commands.Bot(command_prefix=prefix)
conn = sqlite3.connect('database.db') dbconnection = sqlite3.connect('database.db')
c = conn.cursor() dbcursor = dbconnection.cursor()
try: #try to create the database tables, if it fails they were created previosly try: #try to create the database tables, if it fails they were created previosly
sql = "CREATE TABLE MESSAGES(user LONG, server LONG, textXP LONG, reactionXP LONG, vcXP LONG)" sql = "CREATE TABLE MESSAGES(user LONG, server LONG, textXP LONG, reactionXP LONG, vcXP LONG)"
sql2 = "CREATE TABLE VcActive (user LONG, startTime LONG)" sql2 = "CREATE TABLE VcActive (user LONG, startTime LONG)"
sql3 = "CREATE TABLE poll (pollID LONG, reaction STRING, calls LONG)" sql3 = "CREATE TABLE poll (pollID LONG, reaction STRING, calls LONG)"
c.execute(sql) dbcursor.execute(sql)
c.execute(sql2) dbcursor.execute(sql2)
c.execute(sql3) dbcursor.execute(sql3)
except: except:
pass pass
@bot.event #print the username and id to the console and change the game status @bot.event #print the username and id to the console and change the game status
@ -37,15 +37,15 @@ async def on_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 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 if(message.author.bot) or (message.author.id == bot.user.id): #check if the bot has written the message, if yes stop parsing it
return return
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 res = dbcursor.execute("SELECT textXP FROM MESSAGES WHERE user=? AND server = ?", [message.author.id, message.guild.id]) #try to get the current textXP of the user
result = res.fetchone() result = res.fetchone()
if (result == None): if (result == None):
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 dbcursor.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
else: else:
messages = result[0] + messageXP messages = result[0] + messageXP
c.execute("UPDATE MESSAGES SET textXP = ? WHERE user = ? AND server = ?", [messages, message.author.id, message.guild.id]) #update the textXP value in the Database dbcursor.execute("UPDATE MESSAGES SET textXP = ? WHERE user = ? AND server = ?", [messages, message.author.id, message.guild.id]) #update the textXP value in the Database
conn.commit() #write the database changes to disk dbconnection.commit() #write the database changes to disk
if message.content.startswith(prefix): #check if the message starts with the prefix, if yes process the command if message.content.startswith(prefix): #check if the message starts with the prefix, if yes process the command
await bot.process_commands(message) await bot.process_commands(message)
@ -57,33 +57,33 @@ async def on_message(message):#this will run on every message
async def on_raw_reaction_add(message): #this runs on every reaction async def on_raw_reaction_add(message): #this runs on every reaction
if(message.user_id == bot.user.id):#check if the bot has written the message, if yes stop parsing it if(message.user_id == bot.user.id):#check if the bot has written the message, if yes stop parsing it
return return
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 res = dbcursor.execute("SELECT reactionXP FROM MESSAGES WHERE user=? AND server = ?", [message.user_id, message.guild_id]) #try to get the reactionXP from the database
result = res.fetchone() result = res.fetchone()
if (result == None): if (result == None):
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 dbcursor.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
messages = reactionXP messages = reactionXP
else: else:
messages = result[0] + reactionXP messages = result[0] + reactionXP
c.execute("UPDATE MESSAGES SET reactionXP = ? WHERE user = ? AND server = ?", [messages, message.user_id, message.guild_id])#update the reactionXP with the new values dbcursor.execute("UPDATE MESSAGES SET reactionXP = ? WHERE user = ? AND server = ?", [messages, message.user_id, message.guild_id])#update the reactionXP with the new values
try: try:
polls[str(message.message_id)] # check if the reaction is on a poll 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 calls = dbcursor.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
c.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls + 1, message.message_id, str(message.emoji)]) dbcursor.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls + 1, message.message_id, str(message.emoji)])
except: except:
pass pass
conn.commit() #write the database changes to disk dbconnection.commit() #write the database changes to disk
@bot.event @bot.event
async def on_raw_reaction_remove(message): async def on_raw_reaction_remove(message):
try: try:
polls[str(message.message_id)] #check if the message is a poll 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 calls = dbcursor.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
c.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls - 1, message.message_id, str(message.emoji)]) dbcursor.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls - 1, message.message_id, str(message.emoji)])
except: except:
pass pass
conn.commit()# write the database changes to disk dbconnection.commit()# write the database changes to disk
@bot.event @bot.event
async def on_voice_state_update(member, before, after):#this runs on every voice chat update (user joins, leaves, go afk, moves, ...) async def on_voice_state_update(member, before, after):#this runs on every voice chat update (user joins, leaves, go afk, moves, ...)
@ -91,31 +91,31 @@ async def on_voice_state_update(member, before, after):#this runs on every voice
return return
if not before.afk and after.afk:#check if the user moved from not afk to afk if not before.afk and after.afk:#check if the user moved from not afk to afk
print(str(member) + " is now AFK") print(str(member) + " is now AFK")
result = c.execute("SELECT startTime FROM VcActive WHERE user=?", [member.id]).fetchone()[0]#if yes, get the join time from the database result = dbcursor.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 dbcursor.execute("DELETE FROM VcActive WHERE user=?", [member.id])#delete the entrie from the voicechat in the database
xp = (time() - result) / minutesPerVcXP#calculate the xp 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 = dbcursor.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
c.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id]) dbcursor.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id])
conn.commit() #write the changes to the database dbconnection.commit() #write the changes to the database
return return
if before.afk and not after.afk: #check if the user moved from afk back to active if before.afk and not after.afk: #check if the user moved from afk back to active
print(str(member) + " is active again") print(str(member) + " is active again")
c.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the table dbcursor.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the table
conn.commit() #write the changes to database dbconnection.commit() #write the changes to database
return return
if(after.channel == None): #check if the user left the voicechat if(after.channel == None): #check if the user left the voicechat
print(str(member) + " left the Voicechat") print(str(member) + " left the Voicechat")
result = c.execute("SELECT startTime FROM VcActive WHERE user = ?", [member.id]).fetchone()[0]#get the join time from database result = dbcursor.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 dbcursor.execute("DELETE FROM VcActive WHERE user=?", [member.id])# delete the join time entry
xp = (time() - result) / 60 / minutesPerVcXP#calculate the XP 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 result = dbcursor.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
c.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id]) dbcursor.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id])
conn.commit()#write the changes to disk dbconnection.commit()#write the changes to disk
return return
elif(before.channel == None): #check if a user joins the voicechat elif(before.channel == None): #check if a user joins the voicechat
print(str(member) + " joined the Voicechat " + str(after.channel)) print(str(member) + " joined the Voicechat " + str(after.channel))
c.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the database dbcursor.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the database
conn.commit()#write the changes to databasr dbconnection.commit()#write the changes to databasr
return return
@bot.command(brief="shows your current XP and level") @bot.command(brief="shows your current XP and level")
@ -123,8 +123,8 @@ async def level(ctx, user : discord.Member=None):#shows your current XP and leve
if (user == None): #check if the message author submitted a different user to check, if not use the author if (user == None): #check if the message author submitted a different user to check, if not use the author
user = ctx.message.author user = ctx.message.author
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 xp = dbcursor.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) unsorted = dbcursor.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 ranking = [] #to this variable we will later add the users sorted
@ -164,7 +164,7 @@ async def level(ctx, user : discord.Member=None):#shows your current XP and leve
@bot.command(brief="shows the leaderboard") @bot.command(brief="shows the leaderboard")
async def leaderboard(ctx): async def leaderboard(ctx):
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) unsorted = dbcursor.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 ranking = [] #to this variable we will later add the users sorted
@ -259,7 +259,7 @@ async def poll(ctx, question, *options: str):#this is a tool to create a poll
react_message = await ctx.message.channel.send(embed=embed)#print the survey and save the message to a variable react_message = await ctx.message.channel.send(embed=embed)#print the survey and save the message to a variable
polls[str(react_message.id)] = react_message#save the react message to the polls array to acces it later polls[str(react_message.id)] = react_message#save the react message to the polls array to acces it later
for reaction in reactions[:len(options)]:#for every reaction add an entry to the reaction database and add it to the message, that the user can click it for reaction in reactions[:len(options)]:#for every reaction add an entry to the reaction database and add it to the message, that the user can click it
c.execute("INSERT INTO poll VALUES (?, ?, 0)", [react_message.id, reaction]) dbcursor.execute("INSERT INTO poll VALUES (?, ?, 0)", [react_message.id, reaction])
await react_message.add_reaction(reaction) await react_message.add_reaction(reaction)
embed.set_footer(text='Poll ID: {}'.format(react_message.id))#add the poll id to the message (needed to get the results) embed.set_footer(text='Poll ID: {}'.format(react_message.id))#add the poll id to the message (needed to get the results)
@ -277,7 +277,7 @@ async def result(ctx, id):#this function prints the result of a poll
options = options.split(" ", 1) options = options.split(" ", 1)
opt_dict[options[0]] = options[0] + " " + options[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 request = dbcursor.execute("SELECT calls, reaction FROM poll WHERE pollID = ?", [id]).fetchall() #get the votes from the database
description = "" description = ""
for results in request:#loop through the entries of the database and make the result for results in request:#loop through the entries of the database and make the result
description = description + "\n" + opt_dict[results[1]] + ": " + str(results[0]) + " votes" description = description + "\n" + opt_dict[results[1]] + ": " + str(results[0]) + " votes"
@ -288,7 +288,7 @@ async def result(ctx, id):#this function prints the result of a poll
await poll_message.edit(embed=embed)#edit the origina lmessage and print the results await poll_message.edit(embed=embed)#edit the origina lmessage and print the results
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) 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)
await ctx.message.channel.send(embed=embed2)#print the result message from one line up await ctx.message.channel.send(embed=embed2)#print the result message from one line up
c.execute("DELETE FROM poll where pollID = ?", [id])#delete the entries from the poll in the database dbcursor.execute("DELETE FROM poll where pollID = ?", [id])#delete the entries from the poll in the database
conn.commit()#write the database to disk dbconnection.commit()#write the database to disk
bot.run(token, bot=botAccount)#start the bot with the options in config.py bot.run(token, bot=botAccount)#start the bot with the options in config.py