gpt4 book ai didi

javascript - 加入服务器时会触发discord.js的channelCreate()事件

转载 作者:行者123 更新时间:2023-12-02 22:21:40 26 4
gpt4 key购买 nike

当机器人加入服务器时,它会触发 2 个事件。第一个是guildCreate 事件,第二个是channelCreate 事件。这是我的事件加载器:

const ascii = require("ascii-table");
let table = new ascii("Events");
table.setHeading("Event", "Load status");
const load = async () => {
const evtFiles = await readdir("./events");
var total = 0;

evtFiles.forEach(file => {
if (file.split(".").slice(-1)[0] !== "js") return;
const evtName = file.split(".")[0];
const event = require(`./events/${file}`);
table.addRow(file, '✅');
client.on(evtName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
total = total +1;

})
console.log(table.toString());
console.log(`Total no of events ${total} loaded ✅`);
}
load();

这是我的 guildCreate 事件:

const Discord = require("discord.js");
const fs = require("fs");
const config1 = require("../config.json");

module.exports = (client, message,guild) => {

console.log(`Hi, ${client.user.username} is now serving in ${client.guilds.size} servers and serving ${client.guilds.reduce((c, p) => c + p.memberCount, 0)} users!.`);


}

这是我的 channel 创建事件:

const Discord = require("discord.js");

module.exports = async ( client , channel ) => {
// event firing when it joins a guild


var log = channel.guild.channels.find(ch => ch.name.includes('member-log'))
var sEmbed = new Discord.RichEmbed()
.setColor("RANDOM")
.setTitle(`:information_source: A channel Was Created!`)
.setDescription(`**Name**\n${channel}`)
.addField(`**Type**`,`${channel.type}`)
.setFooter(`ChannelID:${channel.id} | CreatedAT:${channel.createdAt}`);
log.send(sEmbed);

}

加入公会时我该怎么做才能阻止此channelCreate事件触发?

最佳答案

明白了 Date.now()/1000+2 不会工作,它比这个事件甚至触发 XD 早一点。我这样做了,它的工作效果更好,因为有时如果服务器也有如果 channel 较多,则最好将时间增加到 30 秒,这样更安全

client.on("channelCreate", (channel) => {
let x = channel.guild.me.joinedTimestamp / 1000
if(x <= (x+10)) return; // if the bot just joined the server the channelcreate event will get activated after 10 sec
console.log("Valid event!");
});

关于javascript - 加入服务器时会触发discord.js的channelCreate()事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59206148/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com