StruFu-Bot/Jan/dbtest command.py

78 lines
2.0 KiB
Python

from discord.ext import commands
import random
class sf_user:
def __init__(self, id, username, dmchannel):
self.id = id
self.name = username
self.channel = dmchannel
self.question = 0
self.category = 0
self.semester = 0
bot = commands.Bot(command_prefix="!")
users = []
@bot.event
async def on_ready():
print("we logged in as {0.user.name}".format(bot))
return
@bot.command()
async def start(ctx):
print("hello")
for i in users:
if i.name == ctx.author:
await ctx.send("User already in database")
return
users.append(sf_user(len(users), ctx.author, await ctx.author.create_dm()))
await users[-1].channel.send("Hello")
return
@bot.command()
async def question(ctx):
current_user = 0
for i in users:
if i.channel == ctx.channel:
current_user = i
if current_user == 0:
await ctx.send("User not in database")
return
q_id = random.randint(0, 10)
question_str = get_question(q_id)[0]
users[current_user.id].question = q_id
await current_user.channel.send(question_str)
return
def get_question(q_id):
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.
for line in f:
q_and_a = line.split(|) # Question_and_Answer
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
@bot.command()
async def answer(ctx):
current_user = 0
for i in users:
if i.channel == ctx.channel:
current_user = i
if current_user == 0:
await ctx.send("User not in database")
return
answer = get_question(q_id)[1] # TO DO: return answer
await current_user.channel.send(current_user.question)
return
bot.run("Nzg1MTkxODM3OTg3MzczMDg2.X80Q4g.bK_vMhhESanMOA78KxHfmJft2vk")