Fixed json write
This commit is contained in:
27
test.py
27
test.py
@@ -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,24 +169,20 @@ 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()
|
||||||
|
|
||||||
|
|
||||||
threading.current_thread().name = "main"
|
threading.current_thread().name = "main"
|
||||||
@@ -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)
|
||||||
|
|
||||||
|
|||||||
17
test2.py
17
test2.py
@@ -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')
|
|
||||||
23
test3.py
23
test3.py
@@ -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)
|
|
||||||
Reference in New Issue
Block a user