OTPSystem/discordbot.py
2025-02-22 20:09:07 -06:00

121 lines
5.8 KiB
Python

import discord
from discord.ext import commands
from discord.ext.commands import context
from discord import ui
from mctools import RCONClient
from db import *
from accounts import *
intents = discord.Intents.default()
intents.message_content = True
TOKEN = ''
MY_GUILD = discord.Object(id=)
bot = commands.Bot(command_prefix='$',intents=intents)
intents.messages = True
class check_details(ui.Modal, title="Update/Register Profile Information:"):
def __init__(self, email: str, code: int):
self.email = email
self.otpcode = code
self.mcname_by_email = db.mc_username_lookup(self.email) # NOT for use outside of the self.mcname placeholder
self.placeholder_mc = None # NOT for use outside of the self.mcname placeholder
if (self.mcname_by_email is not False or None):
self.placeholder_mc = self.mcname_by_email
self.otp = ui.TextInput(label=f"OTP Code sent to {self.email}", style=discord.TextStyle.short, required=True, min_length=4, max_length=4, placeholder="eg 0000")
self.mcname = ui.TextInput(label="Minecraft Username (optional)", style=discord.TextStyle.short, required=False, default=self.placeholder_mc)
super().__init__()
self.add_item(self.otp)
self.add_item(self.mcname)
self.custom_id = f"check_details_modal_{self.email}"
async def on_submit(self, interaction: discord.Interaction):
if (accounts.authenticate_account_otp(self.email, self.otp.value)):
if (self.email.endswith("@uwplatt.edu")):
member = interaction.user
role = discord.utils.get(interaction.guild.roles, name="pioneer")
await member.add_roles(role)
host = ""
port = 0000
password = ""
rcon = RCONClient(host, port)
if (db.mc_username_lookup(self.email) != self.mcname.value and db.mc_username_lookup(self.email) != False and db.mc_email_lookup(self.mcname.value) == False):
if rcon.login(password):
old = db.mc_username_lookup(self.email)
if (old is not False):
rcon.command(f"rank {old} guest")
db.add_mc(self.mcname.value, self.email)
rcon.command(f"rank {self.mcname.value} member")
await interaction.response.send_message(f"Profile updated! Email: {self.email}, New/Unchanged MC Username: {self.mcname}")
else:
await interaction.response.send_message("An error occured in deleting the old mc! The account may not be properly registered. Contact an administrator for guidance.")
return
elif (db.mc_email_lookup(self.mcname.value) == False or db.mc_username_lookup(self.email) == False):
if rcon.login(password):
db.add_mc(self.mcname.value, self.email)
rcon.command(f"rank {self.mcname.value} member")
await interaction.response.send_message(f"Profile updated! Email: {self.email}, MC Username: {self.mcname}")
elif (db.mc_email_lookup(self.mcname.value) != self.email):
self.mcname = "Mc name not added/changed due to being taken under a different email. Please contact the administrator if you beleive this was in error."
await interaction.response.send_message(f"Profile updated! Email: {self.email}, MC Username: {self.mcname}")
elif (db.mc_email_lookup(self.mcname.value) is False):
self.mcname = "N/A (user did not specify)"
await interaction.response.send_message(f"Profile updated! Email: {self.email}, MC Username: {self.mcname}")
else:
self.mcname = "An unknown error occured. Try again later."
await interaction.response.send_message(f"Profile updated! Email: {self.email}, MC Username: {self.mcname}")
else:
await interaction.response.send_message("An error occured. Try again or contact the administrator.")
return True
@bot.event
async def on_ready():
print('Logged on as', bot.user)
await bot.tree.sync(guild=MY_GUILD)
@bot.tree.command(name="login", description="login to account", guild=MY_GUILD)
async def login(interaction: discord.Interaction, email: str):
db.init()
member = interaction.user
if (interaction.channel.id != 0):
await interaction.channel.send("This command can only be used in the bot-cmds channel.")
return
if (db.discord_exists(email) is False):
if (db.add_discord(member.id, email)):
if (accounts.create_account_otp(email)):
otpcode = db.get_account_otp(email)
modal = check_details(email=email, code=otpcode)
await interaction.response.send_modal(modal)
else:
await interaction.channel.send(f"Account {email} does not appear to exist! To register, go to https://www.register.uwplattmc.com")
return
elif (int(member.id) == int(db.discord_exists((email)))):
if (accounts.create_account_otp(email)):
otpcode = db.get_account_otp(email)
modal = check_details(email=email, code=otpcode)
await interaction.response.send_modal(modal)
else:
await interaction.channel.send(f"Account {email} does not appear to exist! To register, go to https://www.register.uwplattmc.com")
return
else:
await interaction.channel.send("Only the registered discord account holder may make changes. Contact an if you beleive this is a mistake.")
return
bot.run(TOKEN)