Python如何从服务器转发消息

时间:2018-09-13 02:13:29

标签: python-3.x discord.py

您好,我有一台旧的不和谐服务器,我们现在不使用它。然后我们创建了一个新服务器,并将所有成员移到了那里,但仍有一些成员移到了旧服务器中。因此,可以选择将所有消息从服务器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)

1 个答案:

答案 0 :(得分: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')