【问题标题】:Discord bot messages problemsDiscord 机器人消息问题
【发布时间】: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;


    }
}

【问题讨论】:

    标签: typescript discord discord.js roblox


    【解决方案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;
    
    
        }
    }
    

    【讨论】:

    • 修复后,还是一样的回复。
    猜你喜欢
    • 2019-09-16
    • 2020-08-24
    • 2021-08-19
    • 1970-01-01
    • 2021-01-08
    • 2021-10-27
    • 2021-12-12
    • 2019-04-24
    • 2018-06-18
    相关资源
    最近更新 更多