我有一个正在工作的机器人,它将帖子从我的discord服务器发送到另一个服务器。我正在寻找一种添加其他服务器的额外通道的方法,但不知道如何让它发挥作用。我在某个地方读到get_channel
只工作一次,我需要将它合并到一个循环中?我是python和discord.py的新手,所以很可能我就是不明白。这是我的代码,希望有人能帮忙。
import discord
intents = discord.Intents(messages=True, guilds=True)
from discord.ext import commands
test001from=1029028218684055562
test001to=1028777554217291806, 1028777583808098304
bot = commands.Bot(command_prefix = ".");
@bot.event
async def on_ready():
print("Bot Active")
@bot.event
async def on_message(message):
if message.channel.id == test001from:
channeltosend = bot.get_channel(test001to)
await channeltosend.send(message.content)
bot.run("TOKEN")
首先,将所有通道ID放入一个列表中,如下所示:test001to[1028777554217291806, 1028777583808098304]
其次,在on_ready
函数中使用get_channel
,并将它们保存为新列表。
第三,使用for循环遍历您创建的for chan in channels
的新列表,并将消息发送到每个通道chan.send(message.content)
。