Fixed json write

This commit is contained in:
2022-06-30 02:32:41 +03:00
parent 2f9e202adc
commit d9b4e1e87b
4 changed files with 12 additions and 74 deletions

25
test.py
View File

@@ -22,7 +22,7 @@ DEFAULT_PREFIX = "$" # The prefix you want everyone to have if you don't define
intents = discord.Intents.default() intents = discord.Intents.default()
intents.members = True intents.members = True
bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents) bot = commands.Bot(command_prefix=determine_prefix, guild_subscriptions=True, intents=intents)
inter_client = InteractionClient(bot) inter_client = InteractionClient(bot, sync_commands=True)
class DB: class DB:
@@ -169,23 +169,19 @@ class Commands:
async def write_json(guild: int, param_name: str, param: str or int): async def write_json(guild: int, param_name: str, param: str or int):
with open('prefix.json', 'r') as _f: with open('prefix.json', 'r') as _f:
try: try:
_jsonObject = json.load(_f) _json = json.load(_f)
except json.JSONDecodeError: except json.decoder.JSONDecodeError:
_jsonObject = {} _json = {}
_f.close()
try: try:
_guild_conf = _jsonObject[f'{guild}'] _guild = _json[f'{guild}']
except KeyError as error:
_guild_conf = {f'{guild}': {}}
try:
_param = _guild_conf[f"{param_name}"]
except KeyError: except KeyError:
_param = {f'{param_name}': f'{param}'} _json.update({f'{guild}': {}})
_guild = _json[f'{guild}']
_guild.update({f'{param_name}': f'{param}'})
_jsonObject.update(_guild_conf)
print(_jsonObject)
with open('prefix.json', 'w') as _f: with open('prefix.json', 'w') as _f:
json.dump(_jsonObject, _f) json.dump(_json, _f, indent=4)
_f.close() _f.close()
@@ -310,6 +306,7 @@ async def command_set_prefix(ctx, prefix: str):
Option("prefix", "Specify prefix", OptionType.STRING, required=True), Option("prefix", "Specify prefix", OptionType.STRING, required=True),
] ]
) )
@commands.has_permissions(administrator=True)
async def slash_set_prefix(ctx, prefix: str): async def slash_set_prefix(ctx, prefix: str):
await Commands.set_prefix(ctx, prefix) await Commands.set_prefix(ctx, prefix)

View File

@@ -1,17 +0,0 @@
import json
def write_json(guild: int, param_name: str, param: str or int):
with open('prefix.json', 'r', 'utf-8') as _f:
try:
_json = json.load(_f)
except json.decoder.JSONDecodeError:
_json = {}
_json.update({f'{guild}': {}})
print(_json)
_guild = _json[f'{guild}']
print(_guild)
_guild.update({f'{param_name}': f'{param}'})
print(_json)
write_json(929446191270330452, 'test1', '34')

View File

@@ -1,23 +0,0 @@
my_dict = {'key': 'value'}
print(my_dict)
my_dict.update({'another_key': 'another_value'}) # Дополняем.
print(my_dict)
my_dict.update({'another_key': 'yet_another_value'}) # Обновляем.
print(my_dict)
my_dict.update({"subdict": {}})
subdict = my_dict['subdict']
print(my_dict)
print(subdict)
subdict.update({'subdictkey': 'subdictvalue'})
print(subdict)
print(my_dict)
subdict.update({'subdictkey2': 'subdictvalue2'})
print(my_dict)
subdict.update({'subdictkey': '33'})
print(my_dict)
my_dict.update({'testKey': 'testValue'})
print(my_dict)
print(my_dict)

View File

@@ -1,19 +0,0 @@
from os import walk
f = []
print(f'{f}\n----------------')
for filenames in walk('audio'):
f.extend(filenames)
break
print(f'{f}\n----------------')
f = f[2]
print(f)
print(type(f))
i = 100
print(i)
i = i * 4
print(i)