1
0
Fork 0
mirror of https://gitlab.jonasled.de/jonasled/discordbot synced 2024-11-20 10:13:03 +01: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)
conn = sqlite3.connect('database.db')
c = conn.cursor()
dbconnection = sqlite3.connect('database.db')
dbcursor = dbconnection.cursor()
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)"
sql2 = "CREATE TABLE VcActive (user LONG, startTime LONG)"
sql3 = "CREATE TABLE poll (pollID LONG, reaction STRING, calls LONG)"
c.execute(sql)
c.execute(sql2)
c.execute(sql3)
dbcursor.execute(sql)
dbcursor.execute(sql2)
dbcursor.execute(sql3)
except:
pass
@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
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
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()
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:
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
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
if(message.user_id == bot.user.id):#check if the bot has written the message, if yes stop parsing it
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()
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
else:
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:
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
c.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls + 1, message.message_id, str(message.emoji)])
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
dbcursor.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls + 1, message.message_id, str(message.emoji)])
except:
pass
conn.commit() #write the database changes to disk
dbconnection.commit() #write the database changes to disk
@bot.event
async def on_raw_reaction_remove(message):
try:
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
c.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls - 1, message.message_id, str(message.emoji)])
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
dbcursor.execute("UPDATE poll SET calls = ? WHERE pollID = ? AND reaction = ?", [calls - 1, message.message_id, str(message.emoji)])
except:
pass
conn.commit()# write the database changes to disk
dbconnection.commit()# write the database changes to disk
@bot.event
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
if not before.afk and after.afk:#check if the user moved from not afk to 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
c.execute("DELETE FROM VcActive WHERE user=?", [member.id])#delete the entrie from the voicechat in the database
result = dbcursor.execute("SELECT startTime FROM VcActive WHERE user=?", [member.id]).fetchone()[0]#if yes, get the join time from 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
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
c.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
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
dbcursor.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id])
dbconnection.commit() #write the changes to the database
return
if before.afk and not after.afk: #check if the user moved from afk back to active
print(str(member) + " is active again")
c.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the table
conn.commit() #write the changes to database
dbcursor.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the table
dbconnection.commit() #write the changes to database
return
if(after.channel == None): #check if the user 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
c.execute("DELETE FROM VcActive WHERE user=?", [member.id])# delete the join time entry
result = dbcursor.execute("SELECT startTime FROM VcActive WHERE user = ?", [member.id]).fetchone()[0]#get the join time from database
dbcursor.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
c.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id])
conn.commit()#write the changes to disk
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
dbcursor.execute("UPDATE MESSAGES SET vcXP = ? WHERE user = ? AND server = ?", [result + round(xp), member.id, member.guild.id])
dbconnection.commit()#write the changes to disk
return
elif(before.channel == None): #check if a user joins the voicechat
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
conn.commit()#write the changes to databasr
dbcursor.execute("INSERT INTO VcActive VALUES (?, ?)", [member.id, round(time())])#insert the current time in the database
dbconnection.commit()#write the changes to databasr
return
@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
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
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)
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 = 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
@ -164,7 +164,7 @@ async def level(ctx, user : discord.Member=None):#shows your current XP and leve
@bot.command(brief="shows the leaderboard")
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
@ -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
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
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)
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)
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 = ""
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"
@ -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
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
c.execute("DELETE FROM poll where pollID = ?", [id])#delete the entries from the poll in the database
conn.commit()#write the database to disk
dbcursor.execute("DELETE FROM poll where pollID = ?", [id])#delete the entries from the poll in the database
dbconnection.commit()#write the database to disk
bot.run(token, bot=botAccount)#start the bot with the options in config.py