Seperate data for each guild
This commit is contained in:
parent
20f0756f99
commit
fa88690f92
2 changed files with 24 additions and 8 deletions
|
@ -20,17 +20,17 @@ class Roles(commands.Cog):
|
||||||
@discord.option("role", discord.role.Role, description="Role locked behind a password.")
|
@discord.option("role", discord.role.Role, description="Role locked behind a password.")
|
||||||
@discord.option("password", str, description="Password for given role.")
|
@discord.option("password", str, description="Password for given role.")
|
||||||
async def add(self, ctx, role, password):
|
async def add(self, ctx, role, password):
|
||||||
roles = data.load_data(ROLES_JSON)
|
roles = data.load_guild_data(ctx.author.guild.id, ROLES_JSON)
|
||||||
if password in roles["secret_roles"]:
|
if password in roles["secret_roles"]:
|
||||||
return await ctx.respond(f"Password ``{password}`` is already used.", ephemeral=True)
|
return await ctx.respond(f"Password ``{password}`` is already used.", ephemeral=True)
|
||||||
|
|
||||||
roles["secret_roles"][password] = role.id
|
roles["secret_roles"][password] = role.id
|
||||||
data.dump_data(ROLES_JSON, roles)
|
data.dump_guild_data(ctx.author.guild.id, ROLES_JSON, roles)
|
||||||
return await ctx.respond(f"Secret role {role.mention} added with password {password}.", ephemeral=True)
|
return await ctx.respond(f"Secret role {role.mention} added with password {password}.", ephemeral=True)
|
||||||
|
|
||||||
@secret_roles.command(description="Lists all passwords and their secret roles")
|
@secret_roles.command(description="Lists all passwords and their secret roles")
|
||||||
async def list(self, ctx):
|
async def list(self, ctx):
|
||||||
roles = data.load_data(ROLES_JSON)
|
roles = data.load_guild_data(ctx.author.guild.id, ROLES_JSON)
|
||||||
if len(roles["secret_roles"]) == 0:
|
if len(roles["secret_roles"]) == 0:
|
||||||
return await ctx.respond(f"No current secret roles.", ephemeral=True)
|
return await ctx.respond(f"No current secret roles.", ephemeral=True)
|
||||||
msg = ""
|
msg = ""
|
||||||
|
@ -42,13 +42,13 @@ class Roles(commands.Cog):
|
||||||
@secret_roles.command(description="Deletes given password and its secret role.")
|
@secret_roles.command(description="Deletes given password and its secret role.")
|
||||||
@discord.option("password", str, description="Password to be deleted.")
|
@discord.option("password", str, description="Password to be deleted.")
|
||||||
async def delete(self, ctx, password):
|
async def delete(self, ctx, password):
|
||||||
roles = data.load_data(ROLES_JSON)
|
roles = data.load_guild_data(ctx.author.guild.id, ROLES_JSON)
|
||||||
if password not in roles["secret_roles"]:
|
if password not in roles["secret_roles"]:
|
||||||
return await ctx.respond(f"Role with passowrd {password} does not exist.", ephemeral=True)
|
return await ctx.respond(f"Role with passowrd {password} does not exist.", ephemeral=True)
|
||||||
|
|
||||||
role = get(ctx.author.guild.roles, id=roles["secret_roles"][password])
|
role = get(ctx.author.guild.roles, id=roles["secret_roles"][password])
|
||||||
del roles["secret_roles"][password]
|
del roles["secret_roles"][password]
|
||||||
data.dump_data(ROLES_JSON, roles)
|
data.dump_guild_data(ctx.author.guild.id, ROLES_JSON, roles)
|
||||||
return await ctx.respond(
|
return await ctx.respond(
|
||||||
f"Secret role {role.mention} no longer obtainable with password {password}.",
|
f"Secret role {role.mention} no longer obtainable with password {password}.",
|
||||||
ephemeral=True
|
ephemeral=True
|
||||||
|
@ -57,7 +57,7 @@ class Roles(commands.Cog):
|
||||||
@discord.slash_command(description="Gives a secret role locked by a password.", guild_only=True)
|
@discord.slash_command(description="Gives a secret role locked by a password.", guild_only=True)
|
||||||
@discord.option("password", str, description="Password for secret role.")
|
@discord.option("password", str, description="Password for secret role.")
|
||||||
async def secretrole(self, ctx, password):
|
async def secretrole(self, ctx, password):
|
||||||
roles = data.load_data(ROLES_JSON)
|
roles = data.load_guild_data(ctx.author.guild.id, ROLES_JSON)
|
||||||
if password in roles["secret_roles"]:
|
if password in roles["secret_roles"]:
|
||||||
author = ctx.author
|
author = ctx.author
|
||||||
role = get(author.guild.roles, id=roles["secret_roles"][password])
|
role = get(author.guild.roles, id=roles["secret_roles"][password])
|
||||||
|
|
|
@ -2,15 +2,25 @@ from typing import Any
|
||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
|
|
||||||
|
EXAMPLE_DATA = "data.example"
|
||||||
DATA_FOLDER = "TODO"
|
DATA_FOLDER = "TODO"
|
||||||
|
|
||||||
def load_json(filename: str):
|
def load_json(filename: str):
|
||||||
with open(filename + ".json") as f:
|
filename += ".json"
|
||||||
|
if not os.path.exists(filename):
|
||||||
|
filename = os.path.join(EXAMPLE_DATA, os.path.basename(filename))
|
||||||
|
|
||||||
|
with open(filename) as f:
|
||||||
content = json.load(f)
|
content = json.load(f)
|
||||||
return content
|
return content
|
||||||
|
|
||||||
def dump_json(filename: str, data: Any):
|
def dump_json(filename: str, data: Any):
|
||||||
with open(filename + ".json", "w") as f:
|
filename += ".json"
|
||||||
|
path = os.path.dirname(filename)
|
||||||
|
if not os.path.exists(path):
|
||||||
|
os.makedirs(path)
|
||||||
|
|
||||||
|
with open(filename, "w") as f:
|
||||||
json.dump(data, f)
|
json.dump(data, f)
|
||||||
|
|
||||||
def load_data(filename: str):
|
def load_data(filename: str):
|
||||||
|
@ -18,3 +28,9 @@ def load_data(filename: str):
|
||||||
|
|
||||||
def dump_data(filename: str, data: Any):
|
def dump_data(filename: str, data: Any):
|
||||||
return dump_json(os.path.join(DATA_FOLDER, filename), data)
|
return dump_json(os.path.join(DATA_FOLDER, filename), data)
|
||||||
|
|
||||||
|
def load_guild_data(guild: int, filename: str):
|
||||||
|
return load_data(os.path.join(str(guild), filename))
|
||||||
|
|
||||||
|
def dump_guild_data(guild: int, filename: str, data: Any):
|
||||||
|
return dump_data(os.path.join(str(guild), filename), data)
|
||||||
|
|
Loading…
Reference in a new issue