added persistent user database

This commit is contained in:
Jan Metzler 2020-12-20 22:11:12 +01:00
parent c4014f0fba
commit 1695bfc0f2
3 changed files with 96 additions and 66 deletions

View File

@ -1,6 +1,9 @@
from discord.ext import commands from discord.ext import commands
import asyncio
import random import random
bot = commands.Bot(command_prefix="!")
class sf_user: class sf_user:
def __init__(self, id, username, dmchannel): def __init__(self, id, username, dmchannel):
self.id = id self.id = id
@ -10,8 +13,19 @@ class sf_user:
self.category = 0 self.category = 0
self.semester = 0 self.semester = 0
bot = commands.Bot(command_prefix="!") async def initialize():
users = [] await bot.login("Nzg1MTkxODM3OTg3MzczMDg2.X80Q4g.bK_vMhhESanMOA78KxHfmJft2vk")
userfile = open("users.txt")
userlines = userfile.readlines()
for i in userlines:
userdata = i.split("|")
add_user = await bot.fetch_user(userdata[1])
users.append(sf_user(userdata[0], add_user, await add_user.create_dm()))
userfile.close()
await bot.connect()
@bot.event @bot.event
async def on_ready(): async def on_ready():
@ -25,8 +39,13 @@ async def start(ctx):
if i.name == ctx.author: if i.name == ctx.author:
await ctx.send("User already in database") await ctx.send("User already in database")
return return
users.append(sf_user(len(users), ctx.author, await ctx.author.create_dm())) users.append(sf_user(len(users), ctx.author.id, await ctx.author.create_dm()))
await users[-1].channel.send("Hello") await users[-1].channel.send("Hello")
userfile = open("users.txt", "a")
userfile.write(f"{users[-1].id}|{users[-1].name}|{users[-1].channel}\n")
userfile.close()
return return
@ -49,13 +68,18 @@ async def question(ctx):
def get_question(q_id): def get_question(q_id):
all_lines = [] all_lines = []
with open(../fragen_antworten.txt) as f: # The file is not in the same folder as the program file. Might not work, needs adjustment. with open("fragen_antworten.txt") as f: # The file is not in the same folder as the program file. Might not work, needs adjustment.
for line in f: for line in f:
q_and_a = line.split(|) # Question_and_Answer q_and_a = line.split("|") # Question_and_Answer
all_lines.append(q_and_a) all_lines.append(q_and_a)
return all_lines([q_id][0],[q_id][1]) #[q_id][0] is the question, [q_id][1] is the answer return all_lines([q_id][0],[q_id][1]) #[q_id][0] is the question, [q_id][1] is the answer
@bot.command()
async def debug(ctx):
for i in users:
await ctx.send(f"Question:{i.id}, Name:{i.name}, Channel:{i.channel}")
return
@bot.command() @bot.command()
@ -73,5 +97,10 @@ async def answer(ctx):
await current_user.channel.send(current_user.question) await current_user.channel.send(current_user.question)
return return
#initialization
bot.run("Nzg1MTkxODM3OTg3MzczMDg2.X80Q4g.bK_vMhhESanMOA78KxHfmJft2vk") users = []
loop = asyncio.get_event_loop()
loop.run_until_complete(initialize())
loop.close()

1
Jan/users.txt Normal file
View File

@ -0,0 +1 @@
0|383652565931393029|Direct Message with Muscovy#1562