Немогу понять почему не работает смена префикса?
import random, string
import re
from random import choice
import time
import json
import requests
from itertools import cycle
import random
import os
def get_prefix(client, message): ##first we define get_prefix
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'r') as f: ##we open and read the prefixes.json, assuming it's in the same file
prefixes = json.load(f) #load the json as prefixes
return prefixes[str(message.guild.id)] #recieve the prefix for the guild id given
return prefixes[str(message.guild.id)]
client = commands.Bot(
command_prefix= (get_prefix),
)
@client.event
async def on_guild_join(guild): #when the bot joins the guild
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'r') as f: #read the prefix.json file
prefixes = json.load(f) #load the json file
prefixes[str(guild.id)] = 'bl!'#default prefix
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'w') as f: #write in the prefix.json "message.guild.id": "bl!"
json.dump(prefixes, f, indent=4) #the indent is to make everything look a bit neater
@client.event
async def on_guild_remove(guild): #when the bot is removed from the guild
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'r') as f: #read the file
prefixes = json.load(f)
prefixes.pop(str(guild.id)) #find the guild.id that bot was removed from
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'w') as f: #deletes the guild.id as well as its prefix
json.dump(prefixes, f, indent=4)
@client.command(pass_context=True)
async def changeprefix(ctx, prefix = None): #command: bl!changeprefix ...
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'w') as f: #writes the new prefix into the .json
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}') #confirms the prefix it's been changed to
#next step completely optional: changes bot nickname to also have prefix in the nickname
name=f'{prefix}BotBot'
Ответы (1 шт):
В тексте ошибки написано, что json не может считать json-объект из объекта типа None.
Если вернуться назад по стеку вызовов, то можно увидеть, что ошибка впервые вызывается в строке 22, функции get_prefix() вашего кода:
Такая строчка встречается у вас в каждой функции и будет вызывать ошибку всегда, потому что у вас объект f почему-то принимает значение None
with open('/Users/Користувач/Desktop/Kiris/prefixes.json', 'r') as f:
prefixes = json.load(f)
Это может быть потому что неправильно указан путь к файлу (вероятнее всего, должно быть так):
# добавить букву раздела диска
C:/Users/Користувач/Desktop/Kiris/prefixes.json
Попробуйте вывести объект файла и посмотреть что он из себя представляет print(f)
Или вы можете воспользоваться функцией json.loads(), которая принимает на вход не объект файла, а строку. Тогда код будет выглядеть так
prefixes = json.loads(f.read())
И заодно позволит посмотреть, а удается ли вообще прочитать файл.


