projects-jenz/discord_verificiation/python/discord_verification.py

162 lines
9.5 KiB
Python
Raw Normal View History

#!/home/nonroot/discord_verification/venv/bin/python3
import discord
import math
from discord.ext import commands
from discord.ext.tasks import loop
from discord import HTTPException
from settings import token, get_connection_playtime, get_connection_xenforo
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
def from_steam64(sid):
y = int(sid) - 76561197960265728
x = y % 2
return "STEAM_0:{}:{}".format(x, (y - x) // 2)
@client.event
async def on_message(message):
if message.author.bot:
return
if client.user.mentioned_in(message):
discord_id = message.author.id
target_name = message.author.name
for t in message.mentions:
if not t.bot:
discord_id = t.id
target_name = t.name
break
with get_connection_xenforo() as conn:
with conn.cursor(buffered=True) as cur: #wtf is this buffered shit even
sql_statement = """
select * from unloze_forum_2.xf_user_connected_account e
where e.provider_key = '%s'
"""
cur.execute(sql_statement, [discord_id])
res = cur.fetchone()
if not res:
await message.channel.send(f"{target_name} Your discord account is not associated to any unloze forum account. Create a forum account and associate discord")
else:
sql_statement = """
select * from unloze_forum_2.xf_user_connected_account e
where e.provider like %s and e.user_id = %s and LENGTH(e.provider_key) > 0
"""
cur.execute(sql_statement, ["%steam", res[0]])
res = cur.fetchone()
if not res:
await message.channel.send(f"{target_name} Your discord account is associated to an unloze forum account. But there is no steam account associated. Associate your steam account.")
else:
steam_id64 = res[2].decode("utf-8")
steam_id2 = from_steam64(steam_id64)
with get_connection_playtime() as conn2:
with conn2.cursor(buffered=True) as cur2:
sql_statement = """
select ze_time, mg_time, zr_time, jb_time, discord_allowed from unloze_playtimestats.player_time pt
where pt.steam_id = %s
"""
cur2.execute(sql_statement, [steam_id2])
res = cur2.fetchone()
if not res:
await message.channel.send(f"{target_name} Your steam account has no play time at all on the gameservers. You need 200 hours.")
else:
ze_time = res[0]
mg_time = res[1]
zr_time = res[2]
jb_time = res[3]
allowed = res[4]
if allowed:
await message.channel.send(f"{target_name} You are already allowed into the discord server for surpassing 200 hours game time")
else:
ze_time_hours = math.floor((ze_time / 60) / 60)
ze_time_minutes = math.floor((ze_time / 60) - (ze_time_hours * 60))
mg_time_hours = math.floor((mg_time / 60) / 60)
mg_time_minutes = math.floor((mg_time / 60) - (mg_time_hours * 60))
zr_time_hours = math.floor((zr_time / 60) / 60)
zr_time_minutes = math.floor((zr_time / 60) - (zr_time_hours * 60))
jb_time_hours = math.floor((jb_time / 60) / 60)
jb_time_minutes = math.floor((jb_time / 60) - (jb_time_hours * 60))
total_time_hours = ze_time_hours + mg_time_hours + zr_time_hours + jb_time_hours
total_time_minutes = ze_time_minutes + mg_time_minutes + zr_time_minutes + jb_time_minutes
extra_hour = math.floor(total_time_minutes / 60)
if extra_hour > 0:
total_time_hours += extra_hour
total_time_minutes = math.floor(total_time_minutes - (extra_hour * 60))
await message.channel.send(f"Player: {target_name} \nZE playtime: {ze_time_hours} hours {ze_time_minutes} minutes.\nMG playtime: {mg_time_hours} hours {mg_time_minutes} minutes.\nZR playtime: {zr_time_hours} hours {zr_time_minutes} minutes.\nJB playtime: {jb_time_hours} hours {jb_time_minutes} minutes. \n\nYou need a total of 200 hours gametime for being allowed into the discord automatically: \nTotal Playtime: {total_time_hours} hours {total_time_minutes} minutes.")
@loop(seconds = 10)
async def check_person_to_allow():
global client
d = client
with get_connection_xenforo() as conn:
with conn.cursor(buffered=True) as cur: #wtf is this buffered shit even
for channel in d.get_all_channels():
if channel.name == 'introduction-channel':
for member in channel.members:
discord_id = member.id
sql_statement = """
select * from unloze_forum_2.xf_user_connected_account e
where e.provider_key = '%s'
"""
cur.execute(sql_statement, [discord_id])
res = cur.fetchone()
if res:
sql_statement = """
select * from unloze_forum_2.xf_user_connected_account e
where e.provider like %s and e.user_id = %s and LENGTH(e.provider_key) > 0
"""
cur.execute(sql_statement, ["%steam", res[0]])
res = cur.fetchone()
if res:
steam_id64 = res[2].decode("utf-8")
steam_id2 = from_steam64(steam_id64)
with get_connection_playtime() as conn2:
with conn2.cursor(buffered=True) as cur2:
sql_statement = """
select ze_time, mg_time, zr_time, jb_time from unloze_playtimestats.player_time pt
where pt.steam_id = %s and discord_allowed = 0
"""
cur2.execute(sql_statement, [steam_id2])
res = cur2.fetchone()
if res:
ze_time = res[0]
mg_time = res[1]
zr_time = res[2]
jb_time = res[3]
ze_time = (ze_time / 60) / 60
mg_time = (mg_time / 60) / 60
zr_time = (zr_time / 60) / 60
jb_time = (jb_time / 60) / 60
total_time = ze_time + mg_time + zr_time + jb_time
if total_time > 200.0:
print('member over 200 hours: ', member.name)
sql_statement = """
update unloze_playtimestats.player_time
set discord_allowed = 1 where steam_id = %s
"""
cur2.execute(sql_statement, [steam_id2])
for channel1 in d.get_all_channels():
if channel1.name == 'logs-discord-allowed':
await channel1.send(f"Allowed SteamID {steam_id2} into the discord server using the discord ID: {discord_id} and name {member.name}. Descriminator ID: {member.discriminator}")
try:
await member.remove_roles("new-comer")
except Exception:
pass
break
conn2.commit() #manual comitting should really not be needed. the context manager should handle that if this mysql package was properly made xd
def main():
check_person_to_allow.start()
client.run(token)
if __name__ == '__main__':
main()