繁体   English   中英

如何定义日志通道?

[英]How can I define a log-channel?

我正在尝试从 JSON 读取通道 ID。 有了这个,您应该能够确定应该发送机器人消息的通道,一种日志。 但是,不幸的是,我不知道如何从 JSON 获取单个公会的此 ID。

我的方法:

async def logchannel():
    with open("src/logchannel.json", "r") as f:
        lchannel = json.load(f)

        return lchannel

(在班级顶部说)

    @commands.command(hidden=True)
    @commands.guild_only()
    @commands.has_permissions(manage_messages=True)
    async def setlog(self, ctx, channel: str):
        """Changes the log channel"""
        with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
            log_channel = json.load(fp)

        try:
            log_channel[f"{ctx.channel.id}"] = channel
        except KeyError:
            new = {ctx.channel.id: channel}
            log_channel.update(new)

        await ctx.send(f "Channel set to: `{channel}`")

        with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
            json.dump(log_channel, fpp, indent=2)

然后应该是指定的频道/总是更新自己。

    @commands.command()
    async def cdel(self, ctx, channel: discord.TextChannel):
        with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
            log_channel = json.load(fp)
        await channel.delete()
        await log_channel.send(f "**Successfully deleted channel `{channel}`!**")

这给了我明显的错误AttributeError: 'dict' object has no attribute 'send' 我可能在那里有错误,但没有看到它/它根本无法按照我想要的方式工作。

简而言之:我希望用户能够选择机器人发送的日志通道,就像所有禁止消息等一样。用户本身可以随时通过命令更改通道。 我现在唯一的问题是机器人给了我上面的错误,因为我没有以正确的方式从 JSON 中请求通道。

编辑:这就是我的 JSON 文件的样子:

{
  "811573570831384638": "811578547751616532",
  "811623743959990295": "811573570831384638"
}

第一个数字是执行命令的通道 ID,第二个键是定义的 mod-log 通道。

json.load(fp)将整个 json 文件作为字典获取。 您应该从中获取频道 ID。

@commands.command(hidden=True)
@commands.guild_only()
@commands.has_permissions(manage_messages=True)
async def setlog(self, ctx, channel: discord.TextChannel):
    """Changes the log channel"""
    with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
        log_channel = json.load(fp)

    try:
        log_channel[str(ctx.guild.id)] = channel.id
    except KeyError:
        new = {str(ctx.guild.id): channel.id}
        log_channel.update(new)

    await ctx.send(f"Channel set to: `{channel}`")

    with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
        json.dump(log_channel, fpp, indent=2)

在您的代码中稍作编辑,您可以使用此命令更改日志通道。 您只需提及要创建新日志通道的通道。 它将使用命令的行会的 id 和提到的频道保存为键和值。


当您想向该频道发送消息时,您必须使用guild.get_channel()discord.utils.get() 因为您需要一个discord.TextChannel实例才能将消息发送到此通道。

@commands.command()
async def cdel(self, ctx, channel: discord.TextChannel):
    with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
        log_channel = json.load(fp)
    await channel.delete()
    log_c = ctx.guild.get_channel(log_channel[str(ctx.guild.id)])
    # Or you can use:
    # log_c = discord.utils.get(ctx.guild.text_channels, id=log_channel[str(ctx.guild.id)])
    await log_c.send(f"**Successfully deleted channel `{channel}`!**")

如果要在当前日志通道被删除时更新日志通道 ID,可以使用on_guild_channel_delete事件进行检查。

@client.event
async def on_guild_channel_delete(channel):
    with open('src/logchannel.json', 'r', encoding='utf-8') as fp:
        log_channel = json.load(fp)
    if channel.id in log_channel.values():
        values = list(log_channel.values())
        keys = list(log_channel.keys())
        log_channel[keys[values.index(channel.id)]] = <channel id>
        with open('src/logchannel.json', 'w', encoding='utf-8') as fpp:
            json.dump(log_channel, fpp, indent=2)

如果您删除日志通道,则必须在<channel id>之间放置一个备份通道 id。 因此,如果您删除日志通道,它将自动使用此备份 ID 更改它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM