Some command refactor

This commit is contained in:
Lamp 2018-07-13 14:05:11 -07:00
parent ab6e0e3b49
commit 4f4531357d
No known key found for this signature in database
GPG Key ID: 0F1F8704BEDE369E
5 changed files with 124 additions and 138 deletions

View File

@ -0,0 +1,38 @@
module.exports = {
usage: "<MPP room>",
description: "Creates a bridge to the specified MPP room.",
exec: async function (msg) {
var site = 'MPP';
var room = msg.txt(1);
if (!room) return false;
var existingBridge = (await dbClient.query('SELECT * FROM bridges WHERE mpp_room = $1;', [room])).rows[0];
if (existingBridge) {
if (!existingBridge.disabled) {
return msg.reply(`${site} room ${room} is already bridged.`);
} else {
if (config.disabledRooms.includes(room)) {
return msg.reply(`You cannot bridge this room.`);
} else /* rebridge */ {
let channel = dClient.guilds.get(config.guildID).channels.get(existingBridge.discord_channel_id);
await dbClient.query("UPDATE bridges SET disabled = false WHERE mpp_room = $1", [room]);
await channel.setParent('360557444952227851');
await channel.lockPermissions();
createMPPbridge(room, existingBridge.mpp_room, existingBridge.site, existingBridge.webhook_id, existingBridge.webhook_token);
await msg.reply(`${site} room ${room} has been re-bridged.`);
return;
}
}
}
/* new bridge */
var discordChannelName = room.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
var categoryID = '360557444952227851';
var channel = await dClient.guilds.get(config.guildID).channels.create(discordChannelName, {parent: categoryID});
channel.setTopic(`http://www.multiplayerpiano.com/${encodeURIComponent(room)}`);
var webhook = await channel.createWebhook('Webhook');
createMPPbridge(room, channel.id, site, webhook.id, webhook.token);
dbClient.query('INSERT INTO bridges (site, mpp_room, discord_channel_id, webhook_id, webhook_token, owner_discord_user_id) VALUES ($1, $2, $3, $4, $5, $6)', [
site, room, channel.id, webhook.id, webhook.token, msg.author.id,
]);
msg.reply(`${site} room ${room} is now bridged to ${channel}.`);
}
};

View File

@ -0,0 +1,32 @@
module.exports = {
usage: "<'mpp'/'discord'> <Discord User ID or mention, or MPP _id>",
description: "Changes the MPP or Discord owner of a private bridge. The first argument must be either `mpp` or `discord`.",
aliases: ['changeowner', 'setowner'],
exec: async function (msg) {
if (msg.args.length < 3 || !['mpp','discord'].includes(msg.args[1])) return false;
var res = await dbClient.query('SELECT * FROM bridges WHERE discord_channel_id = $1;', [msg.channel.id]);
if (res.rows.length == 0) return msg.react('🚫');
var bridge = res.rows[0];
if (!(bridge.owner_discord_user_id == msg.author.id || msg.author.id == config.opID)) return msg.react('🚫');
if (msg.args[1] == 'discord') {
let selectedUser = dClient.users.get(msg.args[2]) || msg.mentions.users.first();
if (!selectedUser) return msg.react('⚠️');
msg.channel.overwritePermissions(selectedUser, {
MANAGE_CHANNELS: true,
MANAGE_ROLES: true,
MANAGE_WEBHOOKS: true,
MANAGE_MESSAGES: true
});
let po = msg.channel.permissionOverwrites.find(x => x.id == msg.author.id);
if (po) po.delete();
await dbClient.query('UPDATE bridges SET owner_discord_user_id = $1 WHERE discord_channel_id = $2;', [selectedUser.id, msg.channel.id]);
msg.channel.send(`Ownership of ${msg.channel} has been transferred to ${selectedUser}`);
} else if (msg.args[1] == 'mpp') {
let _id = msg.args[2];
await dbClient.query('UPDATE bridges SET owner_mpp__id = $1 WHERE discord_channel_id = $2;', [_id, msg.channel.id]);
msg.channel.send(`MPP user \`${_id}\` has been assigned as owner of the MPP room, and the crown will be transferred to them whenever possible.`);
//todo give crown if owner there
}
}
};

View File

@ -0,0 +1,23 @@
module.exports = {
description: "Lists online participants",
aliases: ['ppl', 'online'],
exec: async function (message) {
var row = (await dbClient.query("SELECT mpp_room, site FROM bridges WHERE discord_channel_id = $1;", [message.channel.id])).rows[0];
if (!row) {
//message.react('🚫');
message.reply(`Use this in a bridged room to see who is at the other side.`);
return;
}
var ppl = clients[row.site][row.mpp_room].ppl;
var numberOfPpl = Object.keys(ppl).length;
var str = `__**Participants Online (${numberOfPpl})**__\n`;
var names = [];
for (let person in ppl) {
person = ppl[person];
names.push(`\`${person._id.substr(0,6)}\` ${person.name.replace(/<@/g, "<\\@")}`);
}
str += names.join(', ');
message.channel.send(str, {split:{char:''}});
}
};

View File

@ -0,0 +1,27 @@
module.exports = {
usage: "[MPP Room]",
description: "Deletes a bridge to the specified MPP room.",
exec: async function (msg) {
var bridge = (await dbClient.query("SELECT * FROM bridges WHERE mpp_room = $1 OR discord_channel_id = $2", [msg.txt(1), msg.channel.id])).rows[0];
if (!bridge) {
//msg.react('⚠️');
msg.reply(`That room is not bridged. Make sure you type the MPP room name correctly.`);
return;
}
if (bridge.disabled) {
msg.reply(`That room has already been unbridged.`);
return;
}
if (!(bridge.owner_discord_user_id == msg.author.id || msg.author.id == config.opID)) {
//msg.react('🚫');
msg.reply(`You do not own that bridge.`);
return;
}
await dbClient.query("UPDATE bridges SET disabled = 'true' WHERE mpp_room = $1", [bridge.mpp_room]);
clients.MPP[bridge.mpp_room].stop();
var channel = dClient.channels.get(bridge.discord_channel_id)
await channel.setParent('451838300068511745');
await channel.lockPermissions();
msg.reply(`${bridge.mpp_room} has been unbridged.`);
}
};

View File

@ -236,142 +236,8 @@ global.createMPPbridge = function createMPPbridge(room, DiscordChannelID, site =
// commands
commands.bridge = {
usage: "<MPP room>",
description: "Creates a bridge to the specified MPP room.",
exec: async function (msg) {
var site = 'MPP';
var room = msg.txt(1);
if (!room) return false;
var existingBridge = (await dbClient.query('SELECT * FROM bridges WHERE mpp_room = $1;', [room])).rows[0];
if (existingBridge) {
if (!existingBridge.disabled) {
return msg.reply(`${site} room ${room} is already bridged.`);
} else {
if (config.disabledRooms.includes(room)) {
return msg.reply(`You cannot bridge this room.`);
} else /* rebridge */ {
let channel = dClient.guilds.get(config.guildID).channels.get(existingBridge.discord_channel_id);
await dbClient.query("UPDATE bridges SET disabled = false WHERE mpp_room = $1", [room]);
await channel.setParent('360557444952227851');
await channel.lockPermissions();
createMPPbridge(room, existingBridge.mpp_room, existingBridge.site, existingBridge.webhook_id, existingBridge.webhook_token);
await msg.reply(`${site} room ${room} has been re-bridged.`);
return;
}
}
}
/* new bridge */
var discordChannelName = room.replace(/[^a-zA-Z0-9]/g, '-').toLowerCase();
var categoryID = '360557444952227851';
var channel = await dClient.guilds.get(config.guildID).channels.create(discordChannelName, {parent: categoryID});
channel.setTopic(`http://www.multiplayerpiano.com/${encodeURIComponent(room)}`);
var webhook = await channel.createWebhook('Webhook');
createMPPbridge(room, channel.id, site, webhook.id, webhook.token);
dbClient.query('INSERT INTO bridges (site, mpp_room, discord_channel_id, webhook_id, webhook_token, owner_discord_user_id) VALUES ($1, $2, $3, $4, $5, $6)', [
site, room, channel.id, webhook.id, webhook.token, msg.author.id,
]);
msg.reply(`${site} room ${room} is now bridged to ${channel}.`);
}
};
commands.unbridge = {
usage: "[MPP Room]",
description: "Deletes a bridge to the specified MPP room.",
exec: async function (msg) {
var bridge = (await dbClient.query("SELECT * FROM bridges WHERE mpp_room = $1 OR discord_channel_id = $2", [msg.txt(1), msg.channel.id])).rows[0];
if (!bridge) {
//msg.react('⚠️');
msg.reply(`That room is not bridged. Make sure you type the MPP room name correctly.`);
return;
}
if (bridge.disabled) {
msg.reply(`That room has already been unbridged.`);
return;
}
if (!(bridge.owner_discord_user_id == msg.author.id || msg.author.id == config.opID)) {
//msg.react('🚫');
msg.reply(`You do not own that bridge.`);
return;
}
await dbClient.query("UPDATE bridges SET disabled = 'true' WHERE mpp_room = $1", [bridge.mpp_room]);
clients.MPP[bridge.mpp_room].stop();
var channel = dClient.channels.get(bridge.discord_channel_id)
await channel.setParent('451838300068511745');
await channel.lockPermissions();
msg.reply(`${bridge.mpp_room} has been unbridged.`);
}
}
commands.chown = {
usage: "<'mpp'/'discord'> <Discord User ID or mention, or MPP _id>",
description: "Changes the MPP or Discord owner of a private bridge. The first argument must be either `mpp` or `discord`.",
aliases: ['changeowner', 'setowner'],
exec: async function (msg) {
if (msg.args.length < 3 || !['mpp','discord'].includes(msg.args[1])) return false;
var res = await dbClient.query('SELECT * FROM bridges WHERE discord_channel_id = $1;', [msg.channel.id]);
if (res.rows.length == 0) return msg.react('🚫');
var bridge = res.rows[0];
if (!(bridge.owner_discord_user_id == msg.author.id || msg.author.id == config.opID)) return msg.react('🚫');
if (msg.args[1] == 'discord') {
let selectedUser = dClient.users.get(msg.args[2]) || msg.mentions.users.first();
if (!selectedUser) return msg.react('⚠️');
msg.channel.overwritePermissions(selectedUser, {
MANAGE_CHANNELS: true,
MANAGE_ROLES: true,
MANAGE_WEBHOOKS: true,
MANAGE_MESSAGES: true
});
let po = msg.channel.permissionOverwrites.find(x => x.id == msg.author.id);
if (po) po.delete();
await dbClient.query('UPDATE bridges SET owner_discord_user_id = $1 WHERE discord_channel_id = $2;', [selectedUser.id, msg.channel.id]);
msg.channel.send(`Ownership of ${msg.channel} has been transferred to ${selectedUser}`);
} else if (msg.args[1] == 'mpp') {
let _id = msg.args[2];
await dbClient.query('UPDATE bridges SET owner_mpp__id = $1 WHERE discord_channel_id = $2;', [_id, msg.channel.id]);
msg.channel.send(`MPP user \`${_id}\` has been assigned as owner of the MPP room, and the crown will be transferred to them whenever possible.`);
//todo give crown if owner there
}
}
};
commands.list = {
description: "Lists online participants",
aliases: ['ppl', 'online'],
exec: async function (message) {
var row = (await dbClient.query("SELECT mpp_room, site FROM bridges WHERE discord_channel_id = $1;", [message.channel.id])).rows[0];
if (!row) {
//message.react('🚫');
message.reply(`Use this in a bridged room to see who is at the other side.`);
return;
}
var ppl = clients[row.site][row.mpp_room].ppl;
var numberOfPpl = Object.keys(ppl).length;
var str = `__**Participants Online (${numberOfPpl})**__\n`;
var names = [];
for (let person in ppl) {
person = ppl[person];
names.push(`\`${person._id.substr(0,6)}\` ${person.name.replace(/<@/g, "<\\@")}`);
}
str += names.join(', ');
message.channel.send(str, {split:{char:''}});
}
};
commands.bridge = require('./commands/bridge');
commands.unbridge = require('./commands/unbridge');
commands.chown = require('./commands/chown');
commands.list = require('./commands/list');