很抱歉另一个问题,但是有没有办法找到频道在哪里?因此,就像我删除一个名为“ memes”的频道一样,它会重新制作并返回到原来的位置。无论如何要这样做?任何帮助都会很棒!
答案 0 :(得分:1)
下面是一个示例示例,可以大致完成您想要的操作。这不是一个完美的脚本,但我觉得足以为您指明正确的方向。
在on_guild_join
上,此漫游器列出了它可以看到的所有频道。 (请注意,在我的测试中,该机器人具有管理权限,您需要修改个人权限才能使其正常运行)。抓取频道后,它将信息保存到.json文件中。
在on_guild_channel_delete
,on_guild_channel_update
和on_guild_channel_create
上,该漫游器会比较已删除的频道,并根据“预期”频道的字典对其进行检查。如果匹配,则重新创建频道/类别,并将其放入正确的类别。这部分有点麻烦,我没有时间让它完美运行,但这可能是您尝试玩的好练习。
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)