好,所以我有一个名为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] ....
第一个问题可能是范围