Discord 机器人消息问题

Posted

技术标签:

【中文标题】Discord 机器人消息问题【英文标题】:Discord bot messages problems 【发布时间】:2020-07-04 17:25:36 【问题描述】:

我有一个命令检查人员是否存在,然后检查他们是否在组中,然后提升他们。 该命令正在运行并且正在提升它们,但是当我输入提升某人时,它会回复此消息:

它也找不到等级名称并将其替换为undefined

这是我的代码:

import * as Discord from "discord.js";
import  IBotCommand  from "../api";
import * as ConfigFile from "../config";
let express = require("express");
let roblox = require('noblox.js');

export default class Promote implements IBotCommand 

    private readonly _command = "promote";

    help(): string 
        return "Promote members in the Roblox group."
    

    isThisCommand(command: string): boolean 
        return command === this._command;
    


    async runCommand(args: string[], msgObject: Discord.Message, client: Discord.Client): Promise<void> 
        if (!msgObject.member.roles.find("name", "Council")) 
            msgObject.channel.send(`Nice try $msgObject.author.username but you don't have permission to promote members!`)
                .catch(console.error);
            return;

        

        let groupId = 5293276;
        let maximumRank = ConfigFile.config.maximum_rank
        let username = args[0]
        if (username) 
            msgObject.channel.send(`Checking ROBLOX for $username`)
            roblox.getIdFromUsername(username)
                .then(function (id: any) 
                    roblox.getRankInGroup(groupId, id)
                        .then(function (rank: number) 
                            if (maximumRank <= rank) 
                                msgObject.channel.send(`$id is rank $rank and not promotable.`)
                             else 
                                msgObject.channel.send(`$id is rank $rank and promotable.`)
                                roblox.promote(groupId, id)
                                    .then(function (roles:  oldRole:  Name: any; ; newRole:  Name: any; ; ) 
                                        msgObject.channel.send(`Promoted from $roles.oldRole.Name to $roles.newRole.Name`)
                                    ).catch(console.error)
                                        .then(msgObject.channel.send("Failed to promote."))

                            
                        ).catch(console.error)
                            .then(
                                msgObject.channel.send("Couldn't get him in the group."));

                ).catch(console.error)
                .then(
                    msgObject.channel.send(`Sorry, but $username doesn't exist on ROBLOX.`));

         else 
            msgObject.channel.send("Please enter a username.")
        
        return;


    

【问题讨论】:

【参考方案1】:

方法 find('name', 'name') 已从 discord.js 中删除,使用 find(role =&gt; role.name === 'value')

但我建议您使用role.id 进行检查,因为它更安全,您可以像这样进行检查:

if (!msgObject.member.roles.has('roleID')) return  msgObject.channel.send(`Nice try $msgObject.author.username but you don't have permission to promote members!`)

解决您的问题:

import * as Discord from "discord.js";
import  IBotCommand  from "../api";
import * as ConfigFile from "../config";
let express = require("express");
let roblox = require('noblox.js');

export default class Promote implements IBotCommand 

    private readonly _command = "promote";

    help(): string 
        return "Promote members in the Roblox group."
    

    isThisCommand(command: string): boolean 
        return command === this._command;
    


    async runCommand(args: string[], msgObject: Discord.Message, client: Discord.Client): Promise<void> 
        if (!msgObject.member.roles.find(role => role.name === 'Council')) 
            msgObject.channel.send(`Nice try $msgObject.author.username but you don't have permission to promote members!`)
                .catch(console.error);
            return;

        

        let groupId = 5293276;
        let maximumRank = ConfigFile.config.maximum_rank
        let username = args[0]
        if (username) 
            msgObject.channel.send(`Checking ROBLOX for $username`)
            roblox.getIdFromUsername(username)
                .then(function (id: any) 
                    roblox.getRankInGroup(groupId, id)
                        .then(function (rank: number) 
                            if (maximumRank <= rank) 
                                msgObject.channel.send(`$id is rank $rank and not promotable.`)
                             else 
                                msgObject.channel.send(`$id is rank $rank and promotable.`)
                                roblox.promote(groupId, id)
                                    .then(function (roles:  oldRole:  Name: any; ; newRole:  Name: any; ; ) 
                                        msgObject.channel.send(`Promoted from $roles.oldRole.Name to $roles.newRole.Name`)
                                    ).catch(console.error)
                                        .then(msgObject.channel.send("Failed to promote."))

                            
                        ).catch(console.error)
                            .then(
                                msgObject.channel.send("Couldn't get him in the group."));

                ).catch(console.error)
                .then(
                    msgObject.channel.send(`Sorry, but $username doesn't exist on ROBLOX.`));

         else 
            msgObject.channel.send("Please enter a username.")
        
        return;


    

【讨论】:

修复后,还是一样的回复。

以上是关于Discord 机器人消息问题的主要内容,如果未能解决你的问题,请参考以下文章

Discord 机器人删除消息

如何查找机器人在特定频道中发送的消息? [discord.js]

可以发送嵌入消息的 Discord.js 机器人

Discord.js 用于删除所有不是机器人命令的消息

Discord.js:机器人看不到消息

如何检查消息的作者是不是是 Discord 机器人?