【问题标题】:Adding Roles To A member With A command使用命令向成员添加角色
【发布时间】:2019-11-24 14:29:25
【问题描述】:

好吧,所以基本上我试着发出一个命令,当我说 .ar Admin @user 它给了用户 Admin 或者我在 .ar 之后提到的任何角色名称,但它让我失去了脑细胞,我花了这么长时间加上我的 bot 前缀没有工作,因为同样的问题,所以我通过添加前缀我自己绕过它每个命令我都需要帮助并提前致谢

我确实尝试了很多东西,从箭头函数到普通代码,但普通代码只将角色赋予新成员(公会成员)

`bot.on('message', message => {
  let userToModify = message.mentions.members.first();

  if (message.content.startsWith(".ar Admin" + userToModify)){
    userToModify.addRole('Admin');
}

});`

就像我之前所说的,每当我声明一个 var 或 string 然后转到 .startwith("random command" + Var) 它从来没有用过我试过做 让前缀:“!” 然后 .startwith(prefix + "command") 但它从来没有用过我不得不手动添加前缀而不使用 + 我不想在添加角色时做同样的事情

【问题讨论】:

    标签: javascript discord discord.js


    【解决方案1】:

    我会这样做。通过从消息内容中的第一个参数中获取角色来定义角色,并定义成员的操作方式。检查它们是否存在。

    使用命令时,角色必须在@提到的用户之前。

    bot.on("message", message => {
    
            const prefix = '!'; // just an example, change to whatever you want
    
        if (!message.content.startsWith(prefix)) return; // ignores all messages that don't start with the prefix
    
        const args = message.content.slice(prefix.length).trim().split(/ +/g) // creates an array of arguments for each word after the command
        const cmd = args.shift().toLowerCase(); // this makes the command cASe iNseNsiTiVE
    
        if (cmd === 'addroll' || cmd === 'ar') { // can use 'addrole', or 'ar' to run command
    
            let roleToAdd = message.guild.roles.find(r => r.name === `${args[0]}`); // grab the role from the first argument
            let userToModify = message.mentions.members.first(); // grabs the mentioned user
    
            if (roleToAdd === null) {// if role doesn't exist(aka = null)
                return message.reply(`I couldn't find the ${args[0]} role.`);
            }
            if (userToModify === undefined) {// if noone is mentioned after the role
                return message.reply('You must `@mention` a user!');
            }
    
            userToModify.addRole(roleToAdd)// add the role
            return message.reply(`${userToModify} has been given the ${roleToAdd} role.`)
    
    
    
            // more code
        }
    });
    

    久经考验。

    【讨论】:

    • 天啊,即使使用前缀 OMG,它也确实有效,我很高兴顺便说一句,我应该知道如果我将 if 语句变成“!”前缀应该像你一样工作,再次感谢你,兄弟
    • 不过我建议切换到命令处理程序,这会让一切变得更干净、更易于使用。
    • 我不知道如何制作命令处理程序,如果你能帮助我了解它的基础知识会很高兴
    • @hokageminato youtube 上有很多教程,我是用这个制作的:youtube.com/watch?v=AUOb9_aAk7U
    猜你喜欢
    • 2018-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 2021-02-01
    • 1970-01-01
    • 2021-07-06
    • 1970-01-01
    相关资源
    最近更新 更多