问题描述
我正在制作一个不和谐的机器人,它基本上可以检测用户在消息中发送的单词,它会回复一条消息.但问题是我的机器人正在发送垃圾邮件而不是只发送一次.我不确定发生了什么,我在整个互联网上寻找百家乐凯发k8的解决方案但找不到百家乐凯发k8的解决方案,这就是我来这里寻求帮助的原因.
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!')); } });