You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
8.3 KiB
233 lines
8.3 KiB
const GuildM = require('../models/guilds');
|
|
const roleRulesM = require('../models/autoRoleRule')
|
|
|
|
exports.getUser = (io)=>
|
|
{
|
|
return(req,res)=>
|
|
{
|
|
fetch('https://discord.com/api/users/@me',
|
|
{
|
|
headers: {
|
|
authorization: req.headers.authorization,
|
|
},
|
|
})
|
|
.then(result=>result.json())
|
|
.then(response =>
|
|
{
|
|
res.json(response);
|
|
})
|
|
}
|
|
};
|
|
|
|
exports.guildData = (io,bot)=>
|
|
{
|
|
return async (req, res)=>
|
|
{
|
|
let userGuilds = await fetch('https://discord.com/api/users/@me/guilds',
|
|
{
|
|
headers: {
|
|
authorization: req.headers.authorization,
|
|
},
|
|
})
|
|
.then(result => result.json())
|
|
.then(response =>
|
|
{
|
|
|
|
if(response.message) return response.message
|
|
return response.filter(x=>x.owner==true);
|
|
})
|
|
var promises = [];
|
|
if(typeof userGuilds == 'string') return res.json({Error:userGuilds})
|
|
for(var guild of userGuilds) {
|
|
promises.push(new Promise((resolve, reject) => {
|
|
(async (guild)=>
|
|
{
|
|
|
|
var aux = await getSpecificGuildData(guild.id);
|
|
var roleRules = aux ? await getRoleRules(guild.id):null;
|
|
const dataObject =
|
|
{
|
|
id:guild.id,
|
|
music:aux.guild?aux.guild.guild[0].music:false,
|
|
strikes:aux.guild?aux.guild.guild[0].strikes:false,
|
|
name:guild.name,
|
|
icon:guild.icon?`https://cdn.discordapp.com/icons/${guild.id}/${guild.icon}.webp`:'https://cdn3.iconfinder.com/data/icons/popular-services-brands-vol-2/512/discord-512.png',
|
|
features:guild.features,
|
|
hasRem:aux.extra?true:false,
|
|
memberCount:aux.extra?aux.extra.memberCount:null,
|
|
roleRules
|
|
};
|
|
resolve(dataObject);
|
|
|
|
})(guild)
|
|
}));
|
|
}
|
|
Promise.all(promises).then((values) => {
|
|
return res.json(values);
|
|
});
|
|
|
|
}
|
|
async function getSpecificGuildData(guildID)
|
|
{
|
|
var extra = bot.guilds.cache.get(guildID)
|
|
var guild = await GuildM.find({gID:guildID})
|
|
.then((guild,err)=>
|
|
{
|
|
if(err || guild.length==0) return;
|
|
return {guild};
|
|
})
|
|
return {guild, extra};
|
|
}
|
|
async function getRoleRules(guildid)
|
|
{
|
|
return await roleRulesM.find({gID:guildid}).then((err, rules)=>
|
|
{
|
|
if(err || rules.length==0) return
|
|
return rules
|
|
})
|
|
}
|
|
}
|
|
|
|
|
|
exports.updateGuild = (io)=>
|
|
{
|
|
return (req, res)=>
|
|
{
|
|
|
|
GuildM.findOne({gID:req.body.guildID})
|
|
.then(
|
|
(guild, err)=>
|
|
{
|
|
if(err || guild.length) return res.json({Error:'No guild'});
|
|
|
|
guild[`${req.body.property}`] =! guild[`${req.body.property}`];
|
|
GuildM.updateOne({gID:req.body.guildID}, guild).then(()=>
|
|
{
|
|
return res.json(guild);
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
exports.getMessage = (bot)=>
|
|
{
|
|
return async (req, res)=>
|
|
{
|
|
const TextChannels = bot.guilds.cache.get(req.headers.guildid).channels.cache.filter(x=>x.type==0);
|
|
TextChannels.forEach(channel => {
|
|
(async (channel)=>
|
|
{
|
|
var message
|
|
try {
|
|
message = await channel.messages.fetch(req.headers.messageid);
|
|
} catch (error) {
|
|
//message doesn't exist on this channel or at all
|
|
}
|
|
if(message)
|
|
{
|
|
message = message.content
|
|
if(message.split('***').length %2 == 1) message = message.replaceAll('***', '');
|
|
if(message.split('**').length %2 == 1) message = message.replaceAll('**', '');
|
|
if(message.split('*').length %2 == 1) message = message.replaceAll('*', '');
|
|
if(message.split('~~').length %2 == 1) message = message.replaceAll('~~', '');
|
|
if(message.split('```').length %2 == 1) message = message.replaceAll('```', '');
|
|
if(message.split('``').length %2 == 1) message = message.replaceAll('``', '');
|
|
if(message.split('`').length %2 == 1) message = message.replaceAll('`', '');
|
|
res.json({message})
|
|
}
|
|
})(channel)
|
|
});
|
|
}
|
|
}
|
|
exports.getRules = async (req,res)=>
|
|
{
|
|
var payload;
|
|
if(req.headers.guildid)
|
|
{
|
|
payload = await roleRulesM.find({gID:req.headers.guildid})
|
|
.then(rules =>{return rules})
|
|
.catch(()=>{return null})
|
|
return res.json(payload);
|
|
}
|
|
payload = await roleRulesM.findOne({_id:req.headers.id})
|
|
.then(rule=>{return rule})
|
|
.catch(()=>{return null})
|
|
return res.json(payload);
|
|
}
|
|
exports.deleteRule = async (req, res)=>
|
|
{
|
|
var deleted = await roleRulesM.findByIdAndDelete(req.headers.ruleid)
|
|
.then(()=>{return {success:'Done'}})
|
|
.catch(()=>{return {error:'Failed to delete'}});
|
|
res.json(deleted);
|
|
}
|
|
exports.updateRule = (bot)=>
|
|
{
|
|
return async (req, res)=>
|
|
{
|
|
let args = req.headers;
|
|
if(args.isnew == 'true')
|
|
{
|
|
const validationResult = await confirmArgs(args);
|
|
if (validationResult.error) return res.json({error:validationResult.error})
|
|
var rule = new roleRulesM();
|
|
rule.gID = validationResult.gID;
|
|
rule.mID = validationResult.mID;
|
|
rule.roleID = validationResult.roleID;
|
|
rule.roleEmoji = validationResult.roleEmoji;
|
|
rule.roleName = validationResult.roleName;
|
|
rule.save(err=>
|
|
{
|
|
if(err)res.json(err);
|
|
res.json({success:true,rule})
|
|
})
|
|
}
|
|
else
|
|
{
|
|
const validationResult = await confirmArgs(args);
|
|
if (validationResult.error) return res.json({error:validationResult.error})
|
|
roleRulesM.findOne({id:args.id}).then(rule=>
|
|
{
|
|
if(!rule) return res.json({error:'DB Error'});
|
|
rule.gID = validationResult.gID;
|
|
rule.mID = validationResult.mID;
|
|
rule.roleID = validationResult.roleID;
|
|
rule.roleEmoji = validationResult.roleEmoji;
|
|
rule.roleName = validationResult.roleName;
|
|
roleRulesM.updateOne({_id:rule.id}, rule,{upsert:true}).then(updatedG=>
|
|
{
|
|
res.json({success:true, rule})
|
|
})
|
|
|
|
})
|
|
}
|
|
}
|
|
async function confirmArgs(args)
|
|
{
|
|
var mID = args.messageid;
|
|
var roleID = args.roleid;
|
|
var roleEmoji = args.emojiid;
|
|
var gID = args.gid;
|
|
var roleName;
|
|
if(!(mID && roleID && roleEmoji && gID)) return mID?(roleID?(roleEmoji? false: {error:'Emoji ID'}):{error:'Role ID'}):{error:'Message ID'};
|
|
var m = await (async ()=>
|
|
{
|
|
for(const [id, channel] of bot.channels.cache)
|
|
{
|
|
if(channel.type == 0)
|
|
{
|
|
try {
|
|
const exists = await bot.channels.cache.get(id).messages.fetch(mID);
|
|
if(exists) return m = true;
|
|
} catch (error) {}
|
|
}
|
|
}
|
|
})();
|
|
const r = bot.guilds.cache.get(gID).roles.cache.get(roleID);
|
|
const e = bot.guilds.cache.get(gID).emojis.cache.get(roleEmoji);
|
|
if(!m || !r || !e) return m?(r?(e?false:{error:'Emoji ID'}):{error:'Role ID'}):{error:'Message ID'}
|
|
roleName = r.name;
|
|
return {mID, roleID, roleEmoji, roleName, gID}
|
|
}
|
|
}
|