问题描述
我正在制作一个不和谐的机器人,它基本上可以检测用户在消息中发送的单词,它会回复一条消息.但问题是我的机器人正在发送垃圾邮件而不是只发送一次.我不确定发生了什么,我在整个互联网上寻找解决方案但找不到解决方案,这就是我来这里寻求帮助的原因.
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;
推荐答案
您的机器人会检查消息中是否包含单词 orange",如果包含,则发送它喜欢吃的响应 橙色s.由于响应还包含单词 橙色",因此您的机器人会重复此功能,直到您受到速率限制.
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.
确保检查 消息作者是否是一个机器人,如果是,只需使用 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!')); } });