mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-15 01:49:10 +00:00
chore: remove semi
This commit is contained in:
parent
4b75eaa0ab
commit
8fcc0dcbf9
64 changed files with 1073 additions and 1073 deletions
|
@ -1,10 +1,10 @@
|
|||
const Logger = require('../logger');
|
||||
const Logger = require('../logger')
|
||||
|
||||
class Service {
|
||||
constructor(ctx) {
|
||||
this.ctx = ctx;
|
||||
this.log = new Logger(this.constructor.name);
|
||||
this.ctx = ctx
|
||||
this.log = new Logger(this.constructor.name)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Service;
|
||||
module.exports = Service
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
const Service = require('./Service');
|
||||
const DiscordRPC = require('@roleypoly/rpc/discord');
|
||||
const Service = require('./Service')
|
||||
const DiscordRPC = require('@roleypoly/rpc/discord')
|
||||
|
||||
class DiscordRPCService extends Service {}
|
||||
|
|
|
@ -1,30 +1,30 @@
|
|||
const Service = require('./Service');
|
||||
const discord = require('discord.js');
|
||||
const superagent = require('superagent');
|
||||
const Service = require('./Service')
|
||||
const discord = require('discord.js')
|
||||
const superagent = require('superagent')
|
||||
|
||||
class DiscordService extends Service {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
super(ctx)
|
||||
|
||||
this.botToken = process.env.DISCORD_BOT_TOKEN;
|
||||
this.clientId = process.env.DISCORD_CLIENT_ID;
|
||||
this.clientSecret = process.env.DISCORD_CLIENT_SECRET;
|
||||
this.oauthCallback = process.env.OAUTH_AUTH_CALLBACK;
|
||||
this.botCallback = `${ctx.config.appUrl}/api/oauth/bot/callback`;
|
||||
this.appUrl = process.env.APP_URL;
|
||||
this.isBot = process.env.IS_BOT === 'true' || false;
|
||||
this.rootUsers = new Set((process.env.ROOT_USERS || '').split(','));
|
||||
this.botToken = process.env.DISCORD_BOT_TOKEN
|
||||
this.clientId = process.env.DISCORD_CLIENT_ID
|
||||
this.clientSecret = process.env.DISCORD_CLIENT_SECRET
|
||||
this.oauthCallback = process.env.OAUTH_AUTH_CALLBACK
|
||||
this.botCallback = `${ctx.config.appUrl}/api/oauth/bot/callback`
|
||||
this.appUrl = process.env.APP_URL
|
||||
this.isBot = process.env.IS_BOT === 'true' || false
|
||||
this.rootUsers = new Set((process.env.ROOT_USERS || '').split(','))
|
||||
|
||||
this.client = new discord.Client();
|
||||
this.client.options.disableEveryone = true;
|
||||
this.client = new discord.Client()
|
||||
this.client.options.disableEveryone = true
|
||||
|
||||
this.cmds = this._cmds();
|
||||
this.cmds = this._cmds()
|
||||
|
||||
this.startBot();
|
||||
this.startBot()
|
||||
}
|
||||
|
||||
ownGm(server) {
|
||||
return this.gm(server, this.client.user.id);
|
||||
return this.gm(server, this.client.user.id)
|
||||
}
|
||||
|
||||
fakeGm({ id = 0, nickname = '[none]', displayHexColor = '#ffffff' }) {
|
||||
|
@ -35,41 +35,41 @@ class DiscordService extends Service {
|
|||
__faked: true,
|
||||
roles: {
|
||||
has() {
|
||||
return false;
|
||||
return false
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
isRoot(id) {
|
||||
return this.rootUsers.has(id);
|
||||
return this.rootUsers.has(id)
|
||||
}
|
||||
|
||||
async startBot() {
|
||||
await this.client.login(this.botToken);
|
||||
await this.client.login(this.botToken)
|
||||
|
||||
// not all roleypolys are bots.
|
||||
if (this.isBot) {
|
||||
this.log.info('this roleypoly is a bot');
|
||||
this.client.on('message', this.handleMessage.bind(this));
|
||||
this.client.on('guildCreate', this.handleJoin.bind(this));
|
||||
this.log.info('this roleypoly is a bot')
|
||||
this.client.on('message', this.handleMessage.bind(this))
|
||||
this.client.on('guildCreate', this.handleJoin.bind(this))
|
||||
}
|
||||
|
||||
for (let server of this.client.guilds.array()) {
|
||||
await this.ctx.server.ensure(server);
|
||||
await this.ctx.server.ensure(server)
|
||||
}
|
||||
}
|
||||
|
||||
getRelevantServers(userId) {
|
||||
return this.client.guilds.filter(g => g.members.has(userId));
|
||||
return this.client.guilds.filter(g => g.members.has(userId))
|
||||
}
|
||||
|
||||
gm(serverId, userId) {
|
||||
return this.client.guilds.get(serverId).members.get(userId);
|
||||
return this.client.guilds.get(serverId).members.get(userId)
|
||||
}
|
||||
|
||||
getRoles(server) {
|
||||
return this.client.guilds.get(server).roles;
|
||||
return this.client.guilds.get(server).roles
|
||||
}
|
||||
|
||||
getPermissions(gm) {
|
||||
|
@ -77,23 +77,23 @@ class DiscordService extends Service {
|
|||
return {
|
||||
isAdmin: true,
|
||||
canManageRoles: true,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
isAdmin: gm.permissions.hasPermission('ADMINISTRATOR'),
|
||||
canManageRoles: gm.permissions.hasPermission('MANAGE_ROLES', false, true),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
safeRole(server, role) {
|
||||
const r = this.getRoles(server).get(role);
|
||||
return r.editable && !r.hasPermission('MANAGE_ROLES', false, true);
|
||||
const r = this.getRoles(server).get(role)
|
||||
return r.editable && !r.hasPermission('MANAGE_ROLES', false, true)
|
||||
}
|
||||
|
||||
// oauth step 2 flow, grab the auth token via code
|
||||
async getAuthToken(code) {
|
||||
const url = 'https://discordapp.com/api/oauth2/token';
|
||||
const url = 'https://discordapp.com/api/oauth2/token'
|
||||
try {
|
||||
const rsp = await superagent
|
||||
.post(url)
|
||||
|
@ -104,27 +104,27 @@ class DiscordService extends Service {
|
|||
grant_type: 'authorization_code',
|
||||
code: code,
|
||||
redirect_uri: this.oauthCallback,
|
||||
});
|
||||
})
|
||||
|
||||
return rsp.body;
|
||||
return rsp.body
|
||||
} catch (e) {
|
||||
this.log.error('getAuthToken failed', e);
|
||||
throw e;
|
||||
this.log.error('getAuthToken failed', e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
async getUser(authToken) {
|
||||
const url = 'https://discordapp.com/api/v6/users/@me';
|
||||
const url = 'https://discordapp.com/api/v6/users/@me'
|
||||
try {
|
||||
if (authToken == null || authToken === '') {
|
||||
throw new Error('not logged in');
|
||||
throw new Error('not logged in')
|
||||
}
|
||||
|
||||
const rsp = await superagent.get(url).set('Authorization', `Bearer ${authToken}`);
|
||||
return rsp.body;
|
||||
const rsp = await superagent.get(url).set('Authorization', `Bearer ${authToken}`)
|
||||
return rsp.body
|
||||
} catch (e) {
|
||||
this.log.error('getUser error', e);
|
||||
throw e;
|
||||
this.log.error('getUser error', e)
|
||||
throw e
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -153,20 +153,20 @@ class DiscordService extends Service {
|
|||
// returns oauth authorize url with IDENTIFY permission
|
||||
// we only need IDENTIFY because we only use it for matching IDs from the bot
|
||||
getAuthUrl(state) {
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify&state=${state}`;
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&redirect_uri=${this.oauthCallback}&response_type=code&scope=identify&state=${state}`
|
||||
}
|
||||
|
||||
// returns the bot join url with MANAGE_ROLES permission
|
||||
// MANAGE_ROLES is the only permission we really need.
|
||||
getBotJoinUrl() {
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot&permissions=268435456`;
|
||||
return `https://discordapp.com/oauth2/authorize?client_id=${this.clientId}&scope=bot&permissions=268435456`
|
||||
}
|
||||
|
||||
mentionResponse(message) {
|
||||
message.channel.send(
|
||||
`🔰 Assign your roles here! <${this.appUrl}/s/${message.guild.id}>`,
|
||||
{ disableEveryone: true }
|
||||
);
|
||||
)
|
||||
}
|
||||
|
||||
_cmds() {
|
||||
|
@ -174,15 +174,15 @@ class DiscordService extends Service {
|
|||
{
|
||||
regex: /say (.*)/,
|
||||
handler(message, matches, r) {
|
||||
r(matches[0]);
|
||||
r(matches[0])
|
||||
},
|
||||
},
|
||||
{
|
||||
regex: /set username (.*)/,
|
||||
async handler(message, matches) {
|
||||
const { username } = this.client.user;
|
||||
await this.client.user.setUsername(matches[0]);
|
||||
message.channel.send(`Username changed from ${username} to ${matches[0]}`);
|
||||
const { username } = this.client.user
|
||||
await this.client.user.setUsername(matches[0])
|
||||
message.channel.send(`Username changed from ${username} to ${matches[0]}`)
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -200,8 +200,8 @@ class DiscordService extends Service {
|
|||
(acc, g) => acc + g.roles.size,
|
||||
0
|
||||
)}`,
|
||||
];
|
||||
message.channel.send(t.join('\n'));
|
||||
]
|
||||
message.channel.send(t.join('\n'))
|
||||
},
|
||||
},
|
||||
]
|
||||
|
@ -209,51 +209,51 @@ class DiscordService extends Service {
|
|||
.map(({ regex, ...rest }) => ({
|
||||
regex: new RegExp(`^${regex.source}`, regex.flags),
|
||||
...rest,
|
||||
}));
|
||||
}))
|
||||
|
||||
return cmds;
|
||||
return cmds
|
||||
}
|
||||
|
||||
async handleCommand(message) {
|
||||
const cmd = message.content.replace(`<@${this.client.user.id}> `, '');
|
||||
this.log.debug(`got command from ${message.author.username}`, cmd);
|
||||
const cmd = message.content.replace(`<@${this.client.user.id}> `, '')
|
||||
this.log.debug(`got command from ${message.author.username}`, cmd)
|
||||
for (let { regex, handler } of this.cmds) {
|
||||
const match = regex.exec(cmd);
|
||||
const match = regex.exec(cmd)
|
||||
if (match !== null) {
|
||||
this.log.debug('command accepted', { cmd, match });
|
||||
this.log.debug('command accepted', { cmd, match })
|
||||
try {
|
||||
await handler.call(this, message, match.slice(1));
|
||||
return;
|
||||
await handler.call(this, message, match.slice(1))
|
||||
return
|
||||
} catch (e) {
|
||||
this.log.error('command errored', { e, cmd, message });
|
||||
message.channel.send(`❌ **An error occured.** ${e}`);
|
||||
return;
|
||||
this.log.error('command errored', { e, cmd, message })
|
||||
message.channel.send(`❌ **An error occured.** ${e}`)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// nothing matched?
|
||||
this.mentionResponse(message);
|
||||
this.mentionResponse(message)
|
||||
}
|
||||
|
||||
handleMessage(message) {
|
||||
if (message.author.bot && message.channel.type !== 'text') {
|
||||
// drop bot messages and dms
|
||||
return;
|
||||
return
|
||||
}
|
||||
|
||||
if (message.mentions.users.has(this.client.user.id)) {
|
||||
if (this.rootUsers.has(message.author.id)) {
|
||||
this.handleCommand(message);
|
||||
this.handleCommand(message)
|
||||
} else {
|
||||
this.mentionResponse(message);
|
||||
this.mentionResponse(message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async handleJoin(guild) {
|
||||
await this.ctx.server.ensure(guild);
|
||||
await this.ctx.server.ensure(guild)
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = DiscordService;
|
||||
module.exports = DiscordService
|
||||
|
|
|
@ -1,13 +1,13 @@
|
|||
const Service = require('./Service');
|
||||
const LRU = require('lru-cache');
|
||||
const Service = require('./Service')
|
||||
const LRU = require('lru-cache')
|
||||
|
||||
class PresentationService extends Service {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
this.M = ctx.M;
|
||||
this.discord = ctx.discord;
|
||||
super(ctx)
|
||||
this.M = ctx.M
|
||||
this.discord = ctx.discord
|
||||
|
||||
this.cache = new LRU({ max: 500, maxAge: 100 * 60 * 5 });
|
||||
this.cache = new LRU({ max: 500, maxAge: 100 * 60 * 5 })
|
||||
}
|
||||
|
||||
serverSlug(server) {
|
||||
|
@ -16,31 +16,31 @@ class PresentationService extends Service {
|
|||
name: server.name,
|
||||
ownerID: server.ownerID,
|
||||
icon: server.icon,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async oldPresentableServers(collection, userId) {
|
||||
let servers = [];
|
||||
let servers = []
|
||||
|
||||
for (let server of collection.array()) {
|
||||
const gm = server.members.get(userId);
|
||||
const gm = server.members.get(userId)
|
||||
|
||||
servers.push(await this.presentableServer(server, gm));
|
||||
servers.push(await this.presentableServer(server, gm))
|
||||
}
|
||||
|
||||
return servers;
|
||||
return servers
|
||||
}
|
||||
|
||||
async presentableServers(collection, userId) {
|
||||
return collection.array().areduce(async (acc, server) => {
|
||||
const gm = server.members.get(userId);
|
||||
acc.push(await this.presentableServer(server, gm, { incRoles: false }));
|
||||
return acc;
|
||||
});
|
||||
const gm = server.members.get(userId)
|
||||
acc.push(await this.presentableServer(server, gm, { incRoles: false }))
|
||||
return acc
|
||||
})
|
||||
}
|
||||
|
||||
async presentableServer(server, gm, { incRoles = true } = {}) {
|
||||
const sd = await this.ctx.server.get(server.id);
|
||||
const sd = await this.ctx.server.get(server.id)
|
||||
|
||||
return {
|
||||
id: server.id,
|
||||
|
@ -58,7 +58,7 @@ class PresentationService extends Service {
|
|||
message: sd.message,
|
||||
categories: sd.categories,
|
||||
perms: this.discord.getPermissions(gm),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async rolesByServer(server) {
|
||||
|
@ -70,8 +70,8 @@ class PresentationService extends Service {
|
|||
name: r.name,
|
||||
position: r.position,
|
||||
safe: this.discord.safeRole(server.id, r.id),
|
||||
}));
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PresentationService;
|
||||
module.exports = PresentationService
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
const Service = require('./Service');
|
||||
const Service = require('./Service')
|
||||
|
||||
class ServerService extends Service {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
this.Server = ctx.M.Server;
|
||||
this.P = ctx.P;
|
||||
super(ctx)
|
||||
this.Server = ctx.M.Server
|
||||
this.P = ctx.P
|
||||
}
|
||||
|
||||
async ensure(server) {
|
||||
let srv;
|
||||
let srv
|
||||
try {
|
||||
srv = await this.get(server.id);
|
||||
srv = await this.get(server.id)
|
||||
} catch (e) {}
|
||||
|
||||
if (srv == null) {
|
||||
|
@ -18,20 +18,20 @@ class ServerService extends Service {
|
|||
id: server.id,
|
||||
message: '',
|
||||
categories: {},
|
||||
});
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
create({ id, message, categories }) {
|
||||
const srv = this.Server.build({ id, message, categories });
|
||||
const srv = this.Server.build({ id, message, categories })
|
||||
|
||||
return srv.save();
|
||||
return srv.save()
|
||||
}
|
||||
|
||||
async update(id, newData) {
|
||||
const srv = await this.get(id, false);
|
||||
const srv = await this.get(id, false)
|
||||
|
||||
return srv.update(newData);
|
||||
return srv.update(newData)
|
||||
}
|
||||
|
||||
async get(id, plain = true) {
|
||||
|
@ -39,26 +39,26 @@ class ServerService extends Service {
|
|||
where: {
|
||||
id,
|
||||
},
|
||||
});
|
||||
})
|
||||
|
||||
if (!plain) {
|
||||
return s;
|
||||
return s
|
||||
}
|
||||
|
||||
return s.get({ plain: true });
|
||||
return s.get({ plain: true })
|
||||
}
|
||||
|
||||
async getAllowedRoles(id) {
|
||||
const server = await this.get(id);
|
||||
const server = await this.get(id)
|
||||
|
||||
return Object.values(server.categories).reduce((acc, c) => {
|
||||
if (c.hidden !== true) {
|
||||
return acc.concat(c.roles);
|
||||
return acc.concat(c.roles)
|
||||
}
|
||||
|
||||
return acc;
|
||||
}, []);
|
||||
return acc
|
||||
}, [])
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ServerService;
|
||||
module.exports = ServerService
|
||||
|
|
|
@ -1,40 +1,40 @@
|
|||
const Service = require('./Service');
|
||||
const Service = require('./Service')
|
||||
|
||||
class SessionsService extends Service {
|
||||
constructor(ctx) {
|
||||
super(ctx);
|
||||
this.Session = ctx.M.Session;
|
||||
super(ctx)
|
||||
this.Session = ctx.M.Session
|
||||
}
|
||||
|
||||
async get(id, { rolling }) {
|
||||
const user = await this.Session.findOne({ where: { id } });
|
||||
const user = await this.Session.findOne({ where: { id } })
|
||||
|
||||
if (user === null) {
|
||||
return null;
|
||||
return null
|
||||
}
|
||||
|
||||
return user.data;
|
||||
return user.data
|
||||
}
|
||||
|
||||
async set(id, data, { maxAge, rolling, changed }) {
|
||||
let session = await this.Session.findOne({ where: { id } });
|
||||
let session = await this.Session.findOne({ where: { id } })
|
||||
if (session === null) {
|
||||
session = this.Session.build({ id });
|
||||
session = this.Session.build({ id })
|
||||
}
|
||||
|
||||
session.data = data;
|
||||
session.maxAge = maxAge;
|
||||
session.data = data
|
||||
session.maxAge = maxAge
|
||||
|
||||
return session.save();
|
||||
return session.save()
|
||||
}
|
||||
|
||||
async destroy(id) {
|
||||
const sess = await this.Session.findOne({ where: { id } });
|
||||
const sess = await this.Session.findOne({ where: { id } })
|
||||
|
||||
if (sess != null) {
|
||||
return sess.destroy();
|
||||
return sess.destroy()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = SessionsService;
|
||||
module.exports = SessionsService
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue