Bot 在 Discord.js 中作为前缀提及 [英] Bot mention as a prefix in Discord.js

查看:23
本文介绍了Bot 在 Discord.js 中作为前缀提及的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的前缀只有在我不在整个命令中添加空格时才有效,例如:

My prefix only works if I do not add spaces to the entire command, example:

{
    "token": "",
    "prefix": "<@453463055741747200>"
}


const Discord = require("discord.js");

module.exports.run = async (bot, message, args) => {
    let something = args.join(" ");
    message.delete().catch();
    message.channel.send(something);
}

module.exports.help = {
    name: "say"
}

假设我的机器人名称是 MyBot,上面的代码只适用于 @MyBot 说这个,当命令是 时我怎样才能使它工作@MyBot 这么说?

Let's say my bot name is MyBot, the above code would only work with @MyBot say this, how can I make it work when the command is @MyBot say this?

推荐答案

也许这行不通,因为我不使用命令处理程序所以我有不同的代码风格,但你可以尝试我使用的允许我的机器人与多个全局前缀一起使用:

Maybe this won't work, because I don't use a command handler so I have a different code style, but you can try what I use to allow my bot to be used with multiple global prefixes:

var prefixes = require('./prefixes.json')
//in your case can only be var prefixes = ["<@453463055741747200>", "<@!453463055741747200>"]

let prefix = false;
for (const thisPrefix of prefixes) {
    if (message.content.toLowerCase().startsWith(thisPrefix)) prefix = thisPrefix;
}

所以消息只需要以所需的前缀开头.另外,我添加了两个提及前缀,因为不和谐是愚蠢的,并且有两种类型的用户提及:昵称提及和正常提及.因此,在您的代码中,如果机器人有昵称,它将无法工作.这就是我添加 <@!453463055741747200> 的原因.希望对你有帮助

So the message just needs to start with the desired prefix. Also, I added two mention prefixes because discord is dumb and has two types of user mentions: nickname mentions and normal mentions. So in your code the bot will not work if it has a nick. That's why I added <@!453463055741747200> as well. Hope this helps you

这篇关于Bot 在 Discord.js 中作为前缀提及的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆