changed permission management for high-level commands
added a feature to manually whitelist people (and to not whitelist everyone by default) Signed-off-by: SpagettiFisch <johmewu@gmail.com>
This commit is contained in:
parent
13146256ba
commit
82a21ec8c3
3 changed files with 79 additions and 18 deletions
27
main.py
27
main.py
|
|
@ -11,34 +11,41 @@ from discord.ext import slash
|
||||||
token = init.config().get_token()
|
token = init.config().get_token()
|
||||||
domain = init.config().get_pterodactyl_domain()
|
domain = init.config().get_pterodactyl_domain()
|
||||||
apikey = init.config().get_pterodactyl_apikey()
|
apikey = init.config().get_pterodactyl_apikey()
|
||||||
guild_id = init.config().get_guild_id()
|
|
||||||
|
|
||||||
bot = slash.SlashBot(command_prefix='!', help_command=None)
|
bot = slash.SlashBot(command_prefix='!', help_command=None)
|
||||||
# msg_opt = slash.Option(description="Dein Minecraft Name", , required=True)
|
|
||||||
|
|
||||||
@bot.event
|
@bot.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print('Bot wurde gestartet')
|
print('Bot started succesfully')
|
||||||
return
|
return
|
||||||
|
|
||||||
@bot.slash_cmd(aliases=["hilfe"])
|
@bot.slash_cmd(aliases=["hilfe"])
|
||||||
async def help(ctx:slash.Context):
|
async def help(ctx:slash.Context):
|
||||||
"Hilfe für alle verwendbaren Befehle"
|
"Hilfe für alle verwendbaren Befehle" #Help for all usable commands
|
||||||
await functions.cmdhelp(ctx)
|
await functions.cmdhelp(ctx)
|
||||||
|
|
||||||
@bot.slash_cmd(aliases=["minecraft"], guild_id=1210285934248198244)
|
@bot.slash_cmd(aliases=["minecraft"])
|
||||||
async def mc(ctx:slash.Context, name:slash.Option(description="Dein Minecraftname", required=True)):
|
async def mc(ctx:slash.Context, name:slash.Option(description="Dein Minecraftname", required=True)): #Your Minecraft name
|
||||||
"Registriere deinen Minecraft Namen"
|
"Registriere deinen Minecraft Namen" #Register your Minecraft name
|
||||||
await functions.cmdmc(ctx, name.strip(), bot)
|
await functions.cmdmc(ctx, name.strip(), bot)
|
||||||
|
|
||||||
@bot.slash_cmd()
|
@bot.slash_cmd()
|
||||||
async def mcname(ctx:slash.Context):
|
async def mcname(ctx:slash.Context):
|
||||||
"Gibt deinen aktuellen Minecraft Namen an"
|
"Gibt deinen aktuellen Minecraft Namen an" #Outputs your linked Minecraft name
|
||||||
await functions.cmdmcname(ctx)
|
await functions.cmdmcname(ctx)
|
||||||
|
|
||||||
@bot.slash_cmd(guild_id=guild_id, hidden=True)
|
@bot.slash_cmd()
|
||||||
async def shutdown(ctx:slash.Context):
|
async def shutdown(ctx:slash.Context):
|
||||||
"Will shutdown the bot if you are mighty enough."
|
"Will shutdown the bot if you are mighty enough."
|
||||||
await functions.cmdshutdown(ctx, bot)
|
if await functions.isAdmin(ctx, bot):
|
||||||
|
await functions.cmdshutdown(ctx, bot)
|
||||||
|
|
||||||
|
@bot.slash_cmd(guild_id=1210285934248198244)
|
||||||
|
async def allow(ctx:slash.Context, user:slash.Option(description="der zu whitelistene Nutzuer (@<Discordname>)", required=True)):
|
||||||
|
"Fügt Spieler der Whitelist hinzu." #Add Players to whitelist
|
||||||
|
if await functions.isMod(ctx, bot):
|
||||||
|
await functions.cmdallow(ctx, user.strip(), bot)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bot.run(token)
|
bot.run(token)
|
||||||
|
|
@ -11,6 +11,8 @@ from discord.ext import slash
|
||||||
from modules import init
|
from modules import init
|
||||||
|
|
||||||
con, cur = init.getdb()
|
con, cur = init.getdb()
|
||||||
|
mod_roles = init.config().get_mod_roles()
|
||||||
|
admin_roles = init.config().get_admin_roles()
|
||||||
|
|
||||||
async def cmdhelp(ctx:slash.Context):
|
async def cmdhelp(ctx:slash.Context):
|
||||||
embed = discord.Embed(title="Hilfe",
|
embed = discord.Embed(title="Hilfe",
|
||||||
|
|
@ -37,7 +39,8 @@ async def cmdmc(ctx:slash.Context, name:str, client):
|
||||||
cur.execute(f"UPDATE user SET mcname = '{mcinfo['name']}', uuid = '{uuid}' WHERE id = {ctx.author.id}")
|
cur.execute(f"UPDATE user SET mcname = '{mcinfo['name']}', uuid = '{uuid}' WHERE id = {ctx.author.id}")
|
||||||
await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich aktualisiert.')
|
await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich aktualisiert.')
|
||||||
else:
|
else:
|
||||||
cur.execute(f"INSERT INTO user VALUES ({ctx.author.id}, '{ctx.author.nick}', '{ctx.author.avatar_url}', '{mcinfo['name']}', '{uuid}', {True})")
|
cur.execute(f"INSERT INTO user VALUES ({ctx.author.id}, '{ctx.author.nick}', '{ctx.author.avatar_url}', '{mcinfo['name']}', '{uuid}', {False
|
||||||
|
})")
|
||||||
await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich hinzugefügt.')
|
await ctx.respond(f'Dein Minecraftname **{name}** wurde erfolgreich hinzugefügt.')
|
||||||
con.commit()
|
con.commit()
|
||||||
await syncWhitelist()
|
await syncWhitelist()
|
||||||
|
|
@ -69,6 +72,25 @@ async def cmdshutdown(ctx:slash.Context, bot):
|
||||||
else:
|
else:
|
||||||
await ctx.respond('You are not as mighty as you may think you are.')
|
await ctx.respond('You are not as mighty as you may think you are.')
|
||||||
|
|
||||||
|
async def cmdallow(ctx:slash.Context, user:str, bot:discord.ext.commands.Bot):
|
||||||
|
user_id = user.removeprefix('<@').removesuffix('>')
|
||||||
|
try:
|
||||||
|
user = str(await bot.fetch_user(user_id)).removesuffix('#0')
|
||||||
|
except:
|
||||||
|
await ctx.respond(f"Eingabe ungültig. \nErhaltene Eingabe: **{user}**. Es wurde **@<username>** oder **<Nutzer id>** erwartet. \nBitte versuche es erneut.", ephemeral=True)
|
||||||
|
return
|
||||||
|
|
||||||
|
result = cur.execute(f"SELECT * FROM user WHERE id = '{user_id}'")
|
||||||
|
result = cur.fetchone()
|
||||||
|
|
||||||
|
if result:
|
||||||
|
cur.execute(f"UPDATE user SET iswhitelisted = {True} WHERE id = '{user_id}'")
|
||||||
|
con.commit()
|
||||||
|
await ctx.respond(f'Der Nutzer **{user}** wurde erfolgreich gewhitelisted.')
|
||||||
|
await syncWhitelist()
|
||||||
|
else:
|
||||||
|
await ctx.respond(f'Der Nutzer **{user}** existiert nicht im System. Damit er vom System registriert wird, muss er zunächst seinen Minecraftnamen hinzufügen.', ephemeral=True)
|
||||||
|
|
||||||
async def syncWhitelist():
|
async def syncWhitelist():
|
||||||
results = cur.execute("SELECT mcname, uuid, iswhitelisted FROM user")
|
results = cur.execute("SELECT mcname, uuid, iswhitelisted FROM user")
|
||||||
results = cur.fetchall()
|
results = cur.fetchall()
|
||||||
|
|
@ -105,3 +127,35 @@ async def syncWhitelistPterodactyl(whitelist):
|
||||||
async def pterodactylWriteFile(serverid, path, data, apikey):
|
async def pterodactylWriteFile(serverid, path, data, apikey):
|
||||||
url = f'{init.config().get_pterodactyl_domain()}api/client/servers/{serverid}/files/write?file={urllib.parse.quote(path)}'
|
url = f'{init.config().get_pterodactyl_domain()}api/client/servers/{serverid}/files/write?file={urllib.parse.quote(path)}'
|
||||||
requests.post(url, data=data, headers={"Accept": "application/json", "Authorization": f"Bearer {apikey}"})
|
requests.post(url, data=data, headers={"Accept": "application/json", "Authorization": f"Bearer {apikey}"})
|
||||||
|
|
||||||
|
async def isMod(ctx:slash.Context, bot):
|
||||||
|
allowed = False
|
||||||
|
for id in mod_roles:
|
||||||
|
guild = bot.get_guild(ctx.guild.id)
|
||||||
|
role = guild.get_role(id)
|
||||||
|
member = await guild.fetch_member(ctx.author.id)
|
||||||
|
if role in member.roles:
|
||||||
|
allowed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if allowed:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
await ctx.respond("Du hast nicht die nötigen Rechte um diesen Befehl auszuführen.", ephemeral=True) #You are not allowed to perform this command
|
||||||
|
return
|
||||||
|
|
||||||
|
async def isAdmin(ctx:slash.Context, bot):
|
||||||
|
allowed = False
|
||||||
|
for id in admin_roles:
|
||||||
|
guild = bot.get_guild(ctx.guild.id)
|
||||||
|
role = guild.get_role(id)
|
||||||
|
member = await guild.fetch_member(ctx.author.id)
|
||||||
|
if role in member.roles:
|
||||||
|
allowed = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if allowed:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
await ctx.respond("You are not mighty enough to perform this operation.", ephemeral=True)
|
||||||
|
return
|
||||||
|
|
@ -11,8 +11,8 @@ class config():
|
||||||
self.token = p['token']
|
self.token = p['token']
|
||||||
self.pterodactyl_domain = p['pterodactyl_domain']
|
self.pterodactyl_domain = p['pterodactyl_domain']
|
||||||
self.pterodactyl_apikey = p['pterodactyl_apikey']
|
self.pterodactyl_apikey = p['pterodactyl_apikey']
|
||||||
self.guild_id = p['guild_id']
|
self.mod_roles = p['mod_roles']
|
||||||
self.guild_admin_id = p['guild_admin_id']
|
self.admin_roles = p['admin_roles']
|
||||||
|
|
||||||
def get_token(self):
|
def get_token(self):
|
||||||
return self.token
|
return self.token
|
||||||
|
|
@ -20,10 +20,10 @@ class config():
|
||||||
return self.pterodactyl_domain
|
return self.pterodactyl_domain
|
||||||
def get_pterodactyl_apikey(self):
|
def get_pterodactyl_apikey(self):
|
||||||
return self.pterodactyl_apikey
|
return self.pterodactyl_apikey
|
||||||
def get_guild_id(self):
|
def get_mod_roles(self):
|
||||||
return self.guild_id
|
return self.mod_roles
|
||||||
def get_guild_admin_id(self):
|
def get_admin_roles(self):
|
||||||
return self.guild_admin_id
|
return self.admin_roles
|
||||||
|
|
||||||
|
|
||||||
con = sqlite3.connect('data/database.sqlite')
|
con = sqlite3.connect('data/database.sqlite')
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue