Python如何从服务器转发消息

问题描述 投票:1回答:1

您好我有一个旧的discord服务器,我们现在不使用。我们创建了一个新的服务器并将所有成员移动到那里,但仍然有一些成员在旧服务器中。那么是否可以选择将所有消息从服务器A转发到服务器B到特定通道。

更新:我的意思是,当服务器A收到消息时,它应该被发送到服务器B到特定频道。机器人在两个服务器中,所以我可以准确地转发所有传入的消息。

机器代码

token = "xxxxxxxxxxxxx"
prefix = "!"

import discord
from discord.ext import commands
from discord.ext.commands import Bot

bot = commands.Bot(command_prefix=prefix)
bot.remove_command("help")

@bot.event
async def on_ready():
    print('\nLogged in as')
    print("Bot Name: " + bot.user.name)
    print("Bot User ID: " + bot.user.id)

old_server = bot.get_server('xxxxxxxxxxxxx')
new_channel = bot.get_channel('xxxxxxxxxxxxx')

@bot.event
async def on_message(message):
    message.content = message.content.lower()

    if message.server == old_server:
        await bot.send_message(new_channel, message.content)

    await bot.process_commands(message)

bot.run(token)
python-3.x discord.py
1个回答
1
投票

您可以使用on_message事件来检查何时将消息发送到旧服务器并让机器人将消息发布到新服务器。

下面是示例代码,其中机器人将检查旧服务器何时收到消息,然后将相同的消息发布到新服务器上的指定通道。

from discord.ext import commands

client = commands.Bot(command_prefix='!')

@client.event
async def on_message(message):
    old_server = client.get_server('old_server_id')
    new_channel = client.get_channel('new_channel_id')

    if message.server == old_server:
        await client.send_message(new_channel, message.content + ' - ' + message.author.nick)

client.run('token')
© www.soinside.com 2019 - 2024. All rights reserved.