如何在这段selfcore JavaScript代码中使用多个channel_id参数?
实现方案
你只需要将需要监听的频道ID存入数组,通过数组的includes方法判断当前消息的频道ID是否在监听列表中即可完成需求,修改后的代码如下:
import selfcore from "selfcore"; const client = new selfcore(); const gateway = new selfcore.Gateway("ODQ4MDIzNxjc0MDxxx4Mzk0.YS45RA.xxxxxxxxxQYflQE"); // 填入所有需要监听的频道ID const LISTEN_CHANNELS = [ "882134793771425843", "替换为第二个频道ID", "替换为第三个频道ID" ]; // 目标转发Webhook地址 const TARGET_WEBHOOK = "https://discord.com/api/webhooks/xxxxxxxxxx/drO_0pcypQ03HFWAdVN799DpUQKVfUukvf8wM82IZxfGOEK0Mxxxxxxxxxxx"; gateway.on("message", (m) => { // 匹配当前消息是否来自监听列表内的频道 if (LISTEN_CHANNELS.includes(m.channel_id)) { let content = m.content ? m.content : { embeds: [m.embeds[0]] }; client.sendWebhook(TARGET_WEBHOOK, content); } });
扩展:不同频道转发到不同Webhook
如果你需要把不同频道的消息转发到不同的Webhook地址,可以用对象映射的方式实现:
import selfcore from "selfcore"; const client = new selfcore(); const gateway = new selfcore.Gateway("ODQ4MDIzNxjc0MDxxx4Mzk0.YS45RA.xxxxxxxxxQYflQE"); // 键为频道ID,值为对应要转发的Webhook地址 const CHANNEL_WEBHOOK_MAP = { "882134793771425843": "频道1对应Webhook地址", "第二个频道ID": "频道2对应Webhook地址", "第三个频道ID": "频道3对应Webhook地址" }; gateway.on("message", (m) => { const targetWebhook = CHANNEL_WEBHOOK_MAP[m.channel_id]; if (targetWebhook) { let content = m.content ? m.content : { embeds: [m.embeds[0]] }; client.sendWebhook(targetWebhook, content); } });
内容的提问来源于stack exchange,提问作者rahul mahato
相关产品推荐
相关产品推荐

