簡體   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