有没有办法找到频道的位置并将其放回同一个地方?

Is there a way to find where a channel was and put it back to the same place?

抱歉再问一个问题,但有没有办法找到频道所在的位置?就像我删除了一个名为 'memes' 的频道一样,它会重新制作自己并回到原来的位置。无论如何要那样做?任何帮助都会很棒!

下面是一个示例片段,它大致完成了您想要它做的事情。它不是一个完美的工作脚本,但我觉得它足以为您指明正确的方向。

on_guild_join 上,此机器人列出了它可以看到的所有频道。 (请注意,对于我的测试,此机器人具有管理权限,您需要修改您的个人权限才能让它为您工作)。抓取频道后,它会将信息保存到 .json 文件。

on_guild_channel_deleteon_guild_channel_updateon_guild_channel_create 上,机器人比较已删除的频道并将其与 'expected' 频道的字典进行比较。如果匹配,将重新创建 channel/category 并将其放入正确的类别中。这部分有点问题,我没有时间让它完美地工作,但这可能是一个很好的练习,你可以试着玩一下。

import discord
from discord.utils import get
import json

client = discord.Client()
TOKEN = open('token.txt').read()

@client.event
async def on_guild_join(guild):
    """
    Called whenever our bot is invited into the server
    """

    # Create a dictionary to hold our channels
    guild_channels = {}

    # Get all the channels in the guild that was just joined
    for channel in guild.channels:

        # Determine if channel is a category or not
        if channel.category:
            # Store information in our dictionary (if in category)
            guild_channels[channel.name] = {
                                            'channel_name' : channel.name,
                                            'channel_type' : channel.type.name,
                                            'category': channel.category.name
                                          }
        else:
            # Store information in our dictionary (if is category)
            guild_channels[channel.name] = {
                                            'channel_name' : channel.name,
                                            'channel_type' : channel.type.name,
                                            'category': None
                                          }

    # Save our information to a JSON file for persistence
    with open('./{}_channels.json'.format(guild.id), 'w') as gFile:
        json.dump(guild_channels, gFile, indent=4)

@client.event
async def on_guild_channel_delete(channel):
    """
    Called whenever a channel is deleted
    """

    # Get a list of all expected channels
    with open('./{}_channels.json'.format(channel.guild.id), 'r') as gFile:
        expected_channels = json.load(gFile)
    
    # Check if channel was in expected_channels
    if channel.name in expected_channels:

        # If it's a category, remake the category
        if not expected_channels[channel.name]['category']:
            await channel.guild.create_category(expected_channels[channel.name]['channel_name'])

        # If it wasn't a category, make a channel and put it in the category it's supposed to have
        if channel.category:
            if expected_channels[channel.name]['channel_type'] == 'text':
                await channel.guild.create_text_channel(expected_channels[channel.name]['channel_name'], category=get(channel.guild.channels, name=expected_channels[channel.name]['category']))
            if expected_channels[channel.name]['channel_type'] == 'voice':
                await channel.guild.create_voice_channel(expected_channels[channel.name]['channel_name'], category=get(channel.guild.channels, name=expected_channels[channel.name]['category']))

@client.event
async def on_guild_channel_create(channel):
    
    # Get a list of all expected channels
    with open('./{}_channels.json'.format(channel.guild.id), 'r') as gFile:
        expected_channels = json.load(gFile)

    if channel.name in expected_channels:
        if channel.category != expected_channels[channel.name]['category']:
            await channel.edit(category=get(channel.guild.channels, name=expected_channels[channel.name]['category']))

@client.event
async def on_guild_channel_update(before,after):
    
    # Get a list of all expected channels
    with open('./{}_channels.json'.format(before.guild.id), 'r') as gFile:
        expected_channels = json.load(gFile)

    # Move channels to their respective categories
    if before.name in expected_channels:
        if before.category != expected_channels[before.name]['category']:
            await after.edit(category=get(before.guild.channels, name=expected_channels[before.name]['category']))

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

client.run(TOKEN)