简体   繁体   English

如何根据 discord 消息更改变量?

[英]How to change a variable based on a discord message?

I am working on a discord bot, and I am trying to change variables based on user input.我正在研究 discord 机器人,我正在尝试根据用户输入更改变量。 The code should function a little bit like this:代码应该 function 有点像这样:

const w = "something";
const r = "something else";
var a1 = w;

execute(message, args){
    if (!args.length){
        message.channel.send("no arguments");
    } else {
        var args[0] = args[1];
    }
}

What I want to happen is that when a user in discord types我想要发生的是,当 discord 类型的用户

!command a1 r

that the variable a1 changes to "something else".变量 a1 更改为“其他”。 How can I do this?我怎样才能做到这一点?

You can use Object / Map here, instead of variables您可以在此处使用Object / Map代替变量

Something like:就像是:

const consts = {
  w: "something",
  r: "something else"
};

const variables = {
  a1: consts.w
};

execute(message, args){
    if (!args.length){
        message.channel.send("no arguments");
    } else {
        variables[args[0]] = consts[args[1]];
    }
}

However, I'm not sure what you trying to do its good practice.但是,我不确定你试图做些什么好的做法。 Using mutable variables is a big window for bugs, especially if the user can change it.使用可变变量对于错误来说是一个很大的 window,特别是如果用户可以更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM