upgrade test system

This commit is contained in:
derpystuff 2024-01-12 21:38:02 +01:00
parent 79afe37d42
commit 246a64bca4
9 changed files with 76 additions and 83 deletions

View file

@ -1,37 +1,57 @@
const superagent = require('superagent');
// Get Test Config
let LIMITED_TEST_GUILDS;
if(process.env.TESTING_SERVER_IDS) LIMITED_TEST_GUILDS = process.env.TESTING_SERVER_IDS.split(';')
let LIMITED_TEST_CHANNELS;
if(process.env.TESTING_CHANNEL_IDS) LIMITED_TEST_CHANNELS = process.env.TESTING_CHANNEL_IDS.split(';')
let LIMITED_TEST_USERS;
if(process.env.TESTING_USER_IDS) LIMITED_TEST_USERS = process.env.TESTING_USER_IDS.split(';')
let TESTING_REVISION = "-1";
function isLimitedTestGuild(guild){
if(LIMITED_TEST_GUILDS && LIMITED_TEST_GUILDS.includes(guild.id)) return true;
let TESTING_GROUPS = {}
let TESTING_ASSIGNMENTS = {}
function validateGroup(groups = [], featureId){
for(const g of groups){
if(TESTING_GROUPS[g] && TESTING_GROUPS[g].includes(featureId)) return true;
if(TESTING_GROUPS[g] && TESTING_GROUPS[g].includes("*")) return true;
}
return false;
}
function isLimitedTestChannel(channel){
if(LIMITED_TEST_CHANNELS && LIMITED_TEST_CHANNELS.includes(channel.id)) return true;
return false;
// Fetches the testing configuration from the cdn
async function getTestConfig(){
//if(!process.env.TESTING_CONFIG_URL) throw "Missing TESTING_CONFIG_URL in environment";
try{
let config = await superagent.get(process.env.TESTING_CONFIG_URL)
.query({
_t: Date.now()
});
TESTING_GROUPS = config.body.feature_groups
TESTING_ASSIGNMENTS = config.body.feature_assignments
TESTING_REVISION = config.body.revision
console.log('Loaded test configs (revision ' + TESTING_REVISION + ')')
return config.body;
}catch(e){
throw "Unable to retrieve test config."
}
}
function isLimitedTestUser(user){
if(LIMITED_TEST_USERS && LIMITED_TEST_USERS.includes(user.id)) return true;
return false;
}
async function hasFeature(context, feature){
// We need to load the test config first
if(TESTING_REVISION == "-1") await getTestConfig();
// Server
if(context.guild && TESTING_ASSIGNMENTS.servers) if(validateGroup(TESTING_ASSIGNMENTS.servers[context.guild.id], feature)) return true;
// Channel
if(context.channel && TESTING_ASSIGNMENTS.channels) if(validateGroup(TESTING_ASSIGNMENTS.channels[context.channel.id], feature)) return true;
// Category
if(context.channel && context.channel.parent_id && TESTING_ASSIGNMENTS.categories) if(validateGroup(TESTING_ASSIGNMENTS.categories[context.channel.parent_id], feature)) return true;
// User
if(context.user && TESTING_ASSIGNMENTS.users) if(validateGroup(TESTING_ASSIGNMENTS.users[context.user.id], feature)) return true;
function canUseLimitedTestCommands(context){
if(isLimitedTestGuild(context.guild)) return true;
if(isLimitedTestChannel(context.channel)) return true;
if(isLimitedTestUser(context.user)) return true;
return false;
}
module.exports = {
canUseLimitedTestCommands,
isLimitedTestGuild,
isLimitedTestChannel,
isLimitedTestUser
getTestConfig,
hasFeature
}