Discord.js - 不同服务器中的不同变量

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

我有一个机器人,当广播电台开始播放时,会将变量改为true。我想有一个变量,对于不同的服务器来说是独一无二的。我不知道怎么做,这是我的系统代码。

const Radio = {
    Predvaja : false,
    Radio1 : false,
    RadioCity : false,
    RadioDJCity : false,
    RadioCenter : false,
    RadioCenter100 : false,
    RadioMTV : false,
    RadioPtuj : false,
    RadioOtvoreni : false,
    RadioAktual : false,
    RadioVeseljak : false,
    RadioDalmacija : false

}

if (msg === 'radio 1') {
        Radio.Predvaja = true;
        Radio.Radio1 = true;
}
node.js discord.js
1个回答
1
投票

服务器地图

你应该创建一个全局的 Map 对象。请看一下 例子 如何使用它。

Map 存储键值对,通过设计,它被优化为做有效的键值查找。因此,你可以使用服务器ID作为现在正在播放的电台的每条信息的键。

let stationsPerServer = new Map();

//when a server adds your bot
client.on("guildCreate", guild => {
   stationsPerServer.set(guild.id, new Set());
});

//station activation per server
activateStation(guildId, stationName) {
    const stations = stationsPerServer.get(guildId);
    stations.add(stationName);
    //add any other custom actions like broadcast to a voice channel
}

//station deactivation per server
activateStation(guildId, stationName) {
    stationsPerServer.get(guildId).delete(stationName)
    //add any other custom actions like leaving a voice channel
}

//is station playing in server
activateStation(guildId, stationName) {
    return stationsPerServer.get(guildId).has(stationName)
}

//deactivate station for all servers
deactivateStationGlobally(stationName){
    for (let stations of stationsPerServer.values()) {
        stations.delete(stationName);
        //add any other custom actions like leaving the voice channel
    }
}

电台向量

你要存储的基本上是一个二进制向量。你至少有三种选择如何做到这一点。

专用对象

这就是你目前的解决方案。一个充满预定义布尔值的对象。不是很灵活。

使用一组

最好是采取 设置 的台名,现在正在播放。A Set 每一个值只存储一次,并且为快速查找进行了优化。你可以解释 boolean 值作为查询对象结构中是否有一个站存在于活动站集中。

使用集合时有几个优点。1. 你可以在以后很容易地添加其他电台,或者在他们停止广播时删除现有的电台,而不需要改变你的对象结构。3.如果有必要,你可以保留一个所有已知电台的全局集合作为参考。

因此,每台服务器你可以做这样的事情。

let activeStations = new Set(); //initialize the set

//add some stations
//(equals setting the station field to true)
activeStations.add('RadioPtuj');
activeStations.add('RadioDalmacia');
//now the set contains {'RadioPtuj', 'RadioDalmacia'}

//adding a station twice does not change the set
//  a set stores each member just once,
//  unlike an array that does not care about duplicates
activeStations.add('RadioDalmacia');
//the set still contains only {'RadioPtuj', 'RadioDalmacia'}
activeStations.add('RadioDalmacia');
//the set still contains only {'RadioPtuj', 'RadioDalmacia'}

//find out if a station is active
//(equals checking the boolean station field)
activeStations.has('RadioPtuj'); //true
activeStations.has('RadioOtvoreni'); //false

//remove an inactive station
//(equals setting the station field to false)
activeStations.delete('RadioDalmacia');
//now the set contains {'RadioPtuj'}

//deletion of a station not present in the set has no effect
activeStations.delete('RadioMTV');
//the set still contains {'RadioPtuj'}

枚举

Javascript并不支持开箱即用的枚举,但你可以为站点分配二进制代码,并使用二进制操作和掩码来替换一组数字的strnigs。如果你有成千上万的服务器运行你的机器人,这将是相关的。

添加更多的服务器数据

Map 是很好的保存所有的par-server数据,而不仅仅是活动站的集合。我假设您需要存储 dispatcher 对象,一旦你开始将电台串流到语音频道等。你可以将所有这些打包到一个服务器类中,然后让它的 Map 为每个服务器存储这种类的实例。

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