首页 > 解决方案 > JSONDecodeError: Expecting value: line 1 column 1 (char 0) with discord.py after transfering working script to new cog

问题描述

from discord.ext import commands
import discord
import json

d = {"warning1": []}


class Moderation:
def __init__(self, bot):
    self.bot = bot

async def on_ready(self):
    """Prints to the console that the test cog is working"""
    print("Ready in Moderation Cog.")

@commands.command()
async def warn(ctx, user: discord.Member=None):
    """Warns the user, writes the information to a json file and
    if they get warned a second time it kicks them from the server"""
    with open("test.json", "r") as fp:
        liste = json.load(fp)
    if str(user.id) in liste.get("warning1"):
        await ctx.send("User has already been warned once, banning user!")
        await user.kick()
    else:
        with open("test.json", "w") as fp:
            d["warning1"].append(str(user.id))
            json.dump(d, fp)
        await ctx.send("User has been warned")


def setup(bot):
    bot.add_cog(Moderation(bot))

It was working fully before I switched it over to a new cog, so something has broken there, I've also tried async def warn(self, ctx, user: discord.Member=None): instead of without the self, but that still gives the same error message.

Inside the json file looks like this right now:

{"warning1": ["227896738873212929"]}

标签: pythonjsondiscorddiscord.pydiscord.py-rewrite

解决方案


推荐阅读