python - 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"]}
解决方案
推荐阅读
- javascript - 检索多个级别,API
- r - 在用户名中使用带有 @ 符号的包 RCurl ftpUpload
- java - 如何获得大量不同的 jar 文件?
- c# - 如何在删除视图中显示模型项的详细信息
- r - 如何在 R 中自定义数据结果的格式
- browser - 鉴于提供了 --user-agent,网站如何知道客户端不是浏览器?
- c# - 我正在尝试统一创建保存文件,保存和加载功能有效,但未创建保存文件
- ios - 无法设置 RightBarButtonItems 的位置
- python - 如何将包含具有相同值的相同参数子集的 2 个函数组合成 1 个函数?
- c++ - 结构中的常量成员即使在初始化后也返回 0