問題描述
我正在制作一個(gè)不和諧的機(jī)器人,它基本上可以檢測用戶在消息中發(fā)送的單詞,它會(huì)回復(fù)一條消息.但問題是我的機(jī)器人正在發(fā)送垃圾郵件而不是只發(fā)送一次.我不確定發(fā)生了什么,我在整個(gè)互聯(lián)網(wǎng)上尋找解決方案但找不到解決方案,這就是我來這里尋求幫助的原因.
I am making a discord bot that basically detects a word the user sends in a message and it will reply with a message. But the problem is that my bot is spamming the message instead of sending the message just once. I am not sure what is happening, I've looked all over the internet for a solution and couldn't find one which is why I came here for help.
let Discord;
let Database;
if(typeof window !== "undefined"){
Discord = DiscordJS;
Database = EasyDatabase;
} else {
Discord = require("discord.js");
Database = require("easy-json-database");
}
const delay = (ms) => new Promise((resolve) => setTimeout(() => resolve(), ms));
const s4d = {
Discord,
client: null,
tokenInvalid: false,
reply: null,
joiningMember: null,
database: new Database("./db.json"),
checkMessageExists() {
if (!s4d.client) throw new Error('You cannot perform message operations without a Discord.js client')
if (!s4d.client.readyTimestamp) throw new Error('You cannot perform message operations while the bot is not connected to the Discord API')
}
};
s4d.client = new s4d.Discord.Client({
fetchAllMembers: true
});
s4d.client.on('raw', async (packet) => {
if(['MESSAGE_REACTION_ADD', 'MESSAGE_REACTION_REMOVE'].includes(packet.t)){
const guild = s4d.client.guilds.cache.get(packet.d.guild_id);
if(!guild) return;
const member = guild.members.cache.get(packet.d.user_id) || guild.members.fetch(d.user_id).catch(() => {});
if(!member) return;
const channel = s4d.client.channels.cache.get(packet.d.channel_id);
if(!channel) return;
const message = channel.messages.cache.get(packet.d.message_id) || await channel.messages.fetch(packet.d.message_id).catch(() => {});
if(!message) return;
s4d.client.emit(packet.t, guild, channel, message, member, packet.d.emoji.name);
}
});
s4d.client.login('tokenNumber').catch((e) => { s4d.tokenInvalid = true; s4d.tokenError = e; });
s4d.client.on('message', (s4dmessage) => {
if (s4dmessage.content.includes('oranges')) {
s4dmessage.channel.send(String('Yum! I love eating oranges!'));
}
}
);
s4d;
推薦答案
您的機(jī)器人會(huì)檢查消息中是否包含單詞 orange",如果包含,則發(fā)送它喜歡吃的響應(yīng) 橙色s.由于響應(yīng)還包含單詞 橙色",因此您的機(jī)器人會(huì)重復(fù)此功能,直到您受到速率限制.
Your bot checks if the message contains the word "orange" and if it does, sends a response that it loves eating oranges. As the response also contains the word "orange", your bot repeats this function until you're rate limited.
確保檢查 消息作者是否是一個(gè)機(jī)器人,如果是,只需使用 return
退出:
Make sure you check if the message author is a bot, and if it is, simply exit by using return
:
s4d.client.on('message', (s4dmessage) => {
if (s4dmessage.author.bot) return;
if (s4dmessage.content.includes('oranges')) {
s4dmessage.channel.send(String('Yum! I love eating oranges!'));
}
});
這篇關(guān)于Discord bot 在用戶響應(yīng)后無意中發(fā)送垃圾郵件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!