2024-12-18 22:17:51 +01:00
|
|
|
import discord
|
2024-07-24 14:46:46 +02:00
|
|
|
from discord.ext import commands
|
2024-12-18 22:17:51 +01:00
|
|
|
from models.users import *
|
|
|
|
from mojang import API
|
|
|
|
import utils
|
2024-07-24 14:46:46 +02:00
|
|
|
|
2024-12-18 22:17:51 +01:00
|
|
|
color = discord.Color.from_rgb(79, 227, 119)
|
2024-07-24 14:46:46 +02:00
|
|
|
|
|
|
|
class UserManager(commands.Cog):
|
2024-12-18 22:17:51 +01:00
|
|
|
def __init__(self, bot: commands.Bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
def gen_user_info(self, user_name: str, user_id: int):
|
|
|
|
'''
|
|
|
|
Generates user output Embed
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
user_name: str
|
|
|
|
given user name
|
|
|
|
user_id: int
|
|
|
|
discord user id to access database info
|
|
|
|
'''
|
|
|
|
user = User.get(User.username == user_id)
|
|
|
|
embed = discord.Embed(
|
|
|
|
title=user_name if not user.is_admin else f'{user_name} (Admin)',
|
|
|
|
description=f'''
|
|
|
|
Name: {user.mc_name}
|
|
|
|
UUID: {user.mc_uuid}
|
|
|
|
Registered since {user.registration_date.strftime("%d.%m.%Y, %H:%M:%S")}
|
|
|
|
''',
|
|
|
|
color=color,
|
|
|
|
timestamp=utils.now()
|
|
|
|
)
|
|
|
|
return embed
|
|
|
|
|
|
|
|
@commands.hybrid_command(name='info')
|
|
|
|
async def info(self, ctx: commands.Context):
|
|
|
|
'''
|
|
|
|
Registers Users to internal Database
|
|
|
|
and links there minecraft username to there discord account.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ctx: commands.Context
|
|
|
|
The context of the command invocation
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
user = User.get(User.username == ctx.author.id)
|
|
|
|
await ctx.send(embed=self.gen_user_info(ctx.author.name, ctx.author.id))
|
|
|
|
except:
|
|
|
|
await ctx.send(f"{ctx.author.name} isn't registered")
|
|
|
|
|
|
|
|
@commands.hybrid_command(name='register')
|
|
|
|
async def register(self, ctx: commands.Context, minecraft_name: str):
|
|
|
|
'''
|
|
|
|
Registers Users to internal Database
|
|
|
|
and links there minecraft username to there discord account.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ctx: commands.Context
|
|
|
|
The context of the command invocation
|
|
|
|
minecraft_name: str
|
|
|
|
The minecraft user name to link with
|
|
|
|
'''
|
|
|
|
# Get minecraft uuid
|
|
|
|
api = API()
|
|
|
|
uuid = api.get_uuid(minecraft_name)
|
|
|
|
if not uuid:
|
|
|
|
await ctx.send("Username doesn't exist")
|
|
|
|
return
|
|
|
|
|
|
|
|
# build user
|
|
|
|
try:
|
|
|
|
user = User(
|
|
|
|
username=ctx.author.id,
|
|
|
|
mc_name=minecraft_name,
|
|
|
|
mc_uuid=uuid,
|
|
|
|
is_admin=False if not ctx.author.id == 418848241036165160 else True
|
|
|
|
)
|
|
|
|
user.save()
|
|
|
|
except IntegrityError:
|
|
|
|
await ctx.send("You're already registered")
|
|
|
|
return
|
|
|
|
|
|
|
|
await ctx.send(embed=self.gen_user_info(ctx.author.name, ctx.author.id))
|
|
|
|
|
|
|
|
@commands.hybrid_command(name='delete')
|
|
|
|
async def delete(self, ctx: commands.Context):
|
|
|
|
'''
|
|
|
|
Registers Users to internal Database
|
|
|
|
and links there minecraft username to there discord account.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ctx: commands.Context
|
|
|
|
The context of the command invocation
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
user = User.get(User.username == ctx.author.id)
|
|
|
|
user.delete_instance()
|
|
|
|
await ctx.send(f"Purged {ctx.author.name} from database!")
|
|
|
|
except:
|
|
|
|
await ctx.send(f"{ctx.author.name} isn't registered!")
|
|
|
|
|
|
|
|
@commands.hybrid_command(name='op')
|
|
|
|
async def op(self, ctx: commands.Context, member: discord.Member):
|
|
|
|
'''
|
|
|
|
Toggels if User is an Admin
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ctx: commands.Context
|
|
|
|
The context of the command invocation
|
|
|
|
'''
|
|
|
|
user = User.get(User.username == ctx.author.id)
|
|
|
|
if not user.is_admin:
|
|
|
|
await ctx.send("You're not allowed to use this command")
|
|
|
|
return
|
|
|
|
|
|
|
|
if member.id == 418848241036165160:
|
|
|
|
await ctx.send(f"{member.name} is always an admin")
|
|
|
|
return
|
|
|
|
|
|
|
|
user = User.get(User.username == member.id)
|
|
|
|
user.is_admin = not user.is_admin
|
|
|
|
user.save()
|
|
|
|
await ctx.send(embed=self.gen_user_info(member.name, member.id))
|
|
|
|
|
|
|
|
|
|
|
|
@commands.hybrid_command(name='all')
|
|
|
|
async def all(self, ctx: commands.Context):
|
|
|
|
'''
|
|
|
|
Returns User info
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
|
|
|
ctx: commands.Context
|
|
|
|
The context of the command invocation
|
|
|
|
'''
|
|
|
|
try:
|
|
|
|
user = User.get(User.username == ctx.author.id)
|
|
|
|
if not user.is_admin:
|
|
|
|
await ctx.send("You're not allowed to use this command")
|
|
|
|
return
|
|
|
|
except:
|
|
|
|
await ctx.send(f"{ctx.author.name} isn't registered")
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
embed = discord.Embed(
|
|
|
|
title="All Users",
|
|
|
|
description="Registered Users in Database",
|
|
|
|
color=color,
|
|
|
|
timestamp=utils.now()
|
|
|
|
)
|
|
|
|
|
|
|
|
rows = User.select()
|
|
|
|
for row in rows:
|
|
|
|
member = await ctx.guild.fetch_member(row.username)
|
|
|
|
member = member if not row.is_admin else f'{member} (Admin)'
|
|
|
|
reg_date = row.registration_date.strftime("%d.%m.%Y, %H:%M:%S")
|
|
|
|
embed.add_field(name=member, value="{}\n{}\n{}\n".format(row.mc_name, row.mc_uuid, reg_date))
|
|
|
|
await ctx.send(embed=embed)
|