作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当机器人加入服务器时,它会触发 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/
我是一名优秀的程序员,十分优秀!