Bot mention as a prefix in Discord.js

混江龙づ霸主 提交于 2021-02-10 07:06:51

问题


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"
}

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?


回答1:


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;
}

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



来源:https://stackoverflow.com/questions/50698019/bot-mention-as-a-prefix-in-discord-js

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!