向javascript中的字典添加新键(只是对其进行重设)discord.js

问题描述 投票:-1回答:1

好,所以我有一个名为create的命令,我想向名为plans的字典中添加键和值>

因此命令如下所示:!create plan.(plan name) (plan)

这是我的代码:

if (args[1].startsWith('plan.')) { //This checks if the user wants to create a plan
    name = args[1].replace('plan.', '') //This variable is the same as args[1] but plan. is cut off
    plans[name] = args[2] //This is the code i am having touble with but i want it to add a new key and value to plans
    message.channel.send('Added plan for **' + name + '**') //Message from the bot saying that the plan was added
    console.log(args) //All the print statements that i used to find out the problem
    console.log(plans)
    console.log(name)
}

好吧,plans[name] = args[2]是我遇到的麻烦,因为当我写(写给机器人)时发生的是

!create plan.Mod PlanForMod这是从console.log打印出来的结果:

[ 'create', 'plan.Mod', 'PlanForMod' ] { Mod: 'PlanForMod' } Mod

[第一个是args变量,第二个是plans字典,第三个是name变量

这很完美吧?但是,如果我要添加另一个计划,这是我的输出(我输入!create plan.Admin Adminplan

[ 'create', 'plan.Admin', 'Adminplan' ] { Admin: 'Adminplan' } Admin

如您所见,它没有添加其他键,而是用新的键和值替换了整个字典

我还有另一个命令可以打印用户想要的计划

命令:!plan (plan name)

代码:

let myValue = plans[args[2]]
            message.channel.send(myValue)
            console.log(plans)

[如果我键入我在此处显示的内容,然后键入:!plan Mod它表示我无法发送空消息,即使我键入:!create plan.Mod Mod然后!plan Mod也仍然说同样的事情,因为当我用console.log()命令打印出字典时,得到的是:

{} (node:8456) UnhandledPromiseRejectionWarning: DiscordAPIError: Cannot send an empty message at RequestHandler.execute (C:\Users\Bruker\Desktop\BCN\node_modules\discord.js\src\rest\RequestHandler.js:170:25) at processTicksAndRejections (internal/process/task_queues.js:97:5) (node:8456) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag-未处理的拒绝=严格(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3) (node:8456) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

注意空括号吗?它刚浮出水面就可以了!

我该如何解决?

总和:我希望命令添加另一个键,但是它将整个dict替换为新键。如果我在整个字典为空后尝试打印它

[好,所以我有一个名为create的命令,我想向名为plan的字典中添加键和值,因此该命令如下所示:!create plan。(计划名称)(计划)这是我的代码:if( args [1] ....

javascript node.js dictionary discord.js reset
1个回答
0
投票

第一个问题可能是范围

© www.soinside.com 2019 - 2024. All rights reserved.