8 javascript api node.js discord.js
Discord.js 问题 我应该指出我没有使用discord.js 的经验。我有以下代码,应该将用户请求的总和或表达式更改为实际答案和消息返回。我的另一个命令正在工作,但另一个命令不起作用,这是其代码:
client.once("message", msg => {
if(msg.content.includes("!simple")){
math = Number(msg.content.slice(msg.content.search("e")))
msg.reply(guild.username + "The answer is " + math )
}
})
Run Code Online (Sandbox Code Playgroud)
我基本上通过切片方法删除命令部分,然后使用 Number 函数计算它的值,然后返回它,但我没有得到机器人的响应。任何帮助表示赞赏
Rob*_*eiz 22
与此同时,Discord 改变了他们的 API。client.on("message")现在已弃用。2021 年的工作示例如下所示:
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
client.on("messageCreate", (message) => {
if (message.author.bot) return false;
console.log(`Message from ${message.author.username}: ${message.content}`);
});
client.login(process.env.BOT_TOKEN);
Run Code Online (Sandbox Code Playgroud)
引导需要明确的权限才能读取消息。如果机器人没有该权限,则messageCreate不会触发 on 事件。
小智 0
我就是这样做的
client.on("message",message=>{
if(!message.content.startsWith("!simple") return ;//if the message does not start with simple return
const args=message.content.slice(6).trim().split(/ +/);//splits the command into an array separated by spaces
const command=args.shift().toLowerCase();//removes the command (!simple in this case)
//now you can acces your arguments args[0]..args[1]..args[n]
});
Run Code Online (Sandbox Code Playgroud)