@sodacore/discord
Version:
Sodacore Discord is a plugin that offers Discord SSO/OAuth2 support and the ability to create bots in a similar controller pattern.
190 lines (189 loc) • 6.92 kB
JavaScript
import { GuildMember as DiscordGuildMember, User as DiscordUser } from 'discord.js';
import { Utils } from '@sodacore/di';
export function HasRole(roleId) {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method.
methods[methodIndex].auth.push({
wants: 'guildmember',
callback: (member) => {
if (member instanceof DiscordGuildMember) {
return member.roles.cache.has(roleId);
}
return false;
},
});
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}
export function HasRoles(roleIds) {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method.
methods[methodIndex].auth.push({
wants: 'guildmember',
callback: (member) => {
if (member instanceof DiscordGuildMember) {
return roleIds.every(r => member.roles.cache.has(r));
}
return false;
},
});
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}
export function UserCustom(callback) {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method.
methods[methodIndex].auth.push({ wants: 'user', callback: callback });
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}
export function GuildMemberCustom(callback) {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method
methods[methodIndex].auth.push({ wants: 'guildmember', callback: callback });
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}
export function IsDirectMessage() {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method.
methods[methodIndex].auth.push({
wants: 'guildmember',
callback: (member) => member instanceof DiscordUser,
});
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}
export function IsGuildMessage() {
return (target, propertyKey) => {
// Collect the methods and check for a method index.
const methods = Utils.getMeta('methods', 'discord')(target, undefined, []);
let methodIndex = methods.findIndex((m) => m.key === String(propertyKey));
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// If no method index, create a new item.
if (methodIndex === -1) {
methodIndex = methods.push({
key: String(propertyKey),
type: '',
unique: '',
auth: [],
});
}
// Push the changes to this method
methods[methodIndex].auth.push({
wants: 'guildmember',
callback: (member) => member instanceof DiscordGuildMember,
});
// Set the data back.
Utils.setMeta('methods', 'discord')(target, methods);
};
}