使用json python存储频道ID和公会ID

时间:2021-07-08 13:51:32

标签: python json python-3.x database discord

代码

def get_channels(client, guild):
    with open('channels.json','r') as f:
        channelss = json.load(f)

    return channelss[str(guild.id)]


@client.event
async def on_ready():
    print("testing...")


@client.command()
async def setreportchannel(ctx, channels: discord.TextChannel):
    with open('channels.json','r') as f:
        channel = json.load(f)

    channel[str(ctx.guild.id)] = int(channels.id)

    with open('channels.json','w') as f:
        json.dump(channel,f,indent=4)

    await ctx.send(f"set the report channel to {channels}")

@client.command()
async def report(ctx, member:discord.Member, *, reason):
    channel = client.get_channel(get_channels)
    await ctx.channel.send(f"{member} has been reported for: {reason}")

JSON 文件: { “850356901232771082”:850357833538338816 }

问题: 它没有在 json 文件中存储的内容上发送正确的频道,它假设在用户使用命令 setreportchannel 设置频道的地方

1 个答案:

答案 0 :(得分:0)

    def get_channels(client, guild):
    with open('channels.json','r') as f:
        channelss = json.load(f)

    return channelss[str(guild.id)]


@client.event
async def on_ready():
    print("testing...")


@client.command()
async def setreportchannel(ctx, channels: discord.TextChannel):
    with open('channels.json','r') as f:
        channelss = json.load(f)

    channelss[str(ctx.guild.id)] = int(channels.id)

    with open('channels.json','w') as f:
        json.dump(channelss,f,indent=4)

    await ctx.send(f"set the report channel to {channels}")

@client.command()
async def report(ctx, member:discord.Member, *, reason):
    with open('channels.json', 'r') as f:
        channelss = json.load(f)


    channel_log = ctx.guild.get_channel(channelss[str(ctx.guild.id)])
    await channel_log.send(f"{member} has been reported for: {reason}")

所以有人找到了它并帮助了我(不是用勺子喂食)......问题在于我在频道日志中所做的部分,其中我将频道ID而不是公会ID放入其中