Skip to content

Commit

Permalink
Merge pull request #98 from nullabork/fix/bind
Browse files Browse the repository at this point in the history
Fix/bind
  • Loading branch information
zenril authored Nov 24, 2020
2 parents 4d7904e + d50e62d commit b818f5a
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 94 deletions.
2 changes: 1 addition & 1 deletion google.json

Large diffs are not rendered by default.

48 changes: 39 additions & 9 deletions src/commands/modules/bind.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class Bind extends Command {

if (channel && channel.name) {
obj[channel.name] = id;
} else {
obj[id] = 'Channel Not Found';
}

obj[id] = 'Channel Not Found';
});

if (!channelNames || !channelNames.length) {
if (!server.bind || !server.bind) {
obj = [' No Channels Bound!'];
}

Expand Down Expand Up @@ -81,9 +81,16 @@ class Bind extends Command {
server.bindPermit = true;
input.il8nResponse('bind.permit');
} else if (input.args.length > 0) {
for (const arg of input.args) {
if (arg.length > 15) {
if (!server.bind.includes(arg)) server.bind.push(arg);
const mentions = input.message.mentions;
if (mentions.channels.first()) {
mentions.channels.forEach((chan) => {
if (!server.bind.includes(chan.id)) server.bind.push(chan.id);
});
} else {
for (const arg of input.args) {
if (arg.length > 15) {
if (!server.bind.includes(arg)) server.bind.push(arg);
}
}
}

Expand All @@ -100,18 +107,41 @@ class Bind extends Command {
}
}

onUserJoinedChannel({ channelState, member, server }) {
async onUserJoinedChannel({ channelState, member, server }) {
// throw out these states
if (!member.voice.channelID) return;
if (!server.bind.includes(channelState.channelID)) return;
if (!server.bind || !server.bind.length || !server.bind.includes(channelState.channelID)) return;

if (!server.isBound()) {
server.setMaster(member);
server.joinVoiceChannel(member.voice.channel);
await server.joinVoiceChannel(member.voice.channel);
} else if (server.bindPermit) {
server.permit(member.id);
}
}

async onPreValidate({ message, content, server }) {
const { member, channel, mentions } = message;
// throw out these states
if (!member.voice.channelID) return;
if (!server.bind || !server.bind.length || !server.bind.includes(channel.id)) return;
const settings = server.getMemberSettings(message.member);

if (!server.isBound()) {
server.setMaster(member);
const connection = await server.joinVoiceChannel(member.voice.channel);

if (!connection) {
return;
}

setTimeout(() => {
server.talk(content, settings);
}, 600);
} else if (server.bindPermit && !server.permitted[member.id]) {
server.permit(member.id);
}
}
}

//registration
Expand Down
71 changes: 37 additions & 34 deletions src/commands/modules/follow.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var BotCommand = require('@models/BotCommand'),
*
* @return {[undefined]}
* * */
function follow(msg) {
async function follow(msg) {
const server = msg.server;

let member = msg.message.member;
Expand All @@ -39,20 +39,24 @@ function follow(msg) {
};

server.setMaster(member);
server
.joinVoiceChannel(member.voice.channel)
.then(() => {
server.addMemberSetting(member, 'toLanguage', 'default');
commands.notify('follow', { member: member, server: server });
msg.il8nResponse('follow.okay');

// unmute them if they're muted
if (server.getMemberSetting(member, 'muted')) {
server.addMemberSetting(member, 'muted', false);
msg.il8nResponse('mute.unmuted');
}
}, errorFunc)
.catch(errorFunc);

const connection = await server.joinVoiceChannel(member.voice.channel);

if (!connection) {
msg.il8nResponse('follow.error');
server.release();
return;
}

server.addMemberSetting(member, 'toLanguage', 'default');
commands.notify('follow', { member: member, server: server });
msg.il8nResponse('follow.okay');

// unmute them if they're muted
if (server.getMemberSetting(member, 'muted')) {
server.addMemberSetting(member, 'muted', false);
msg.il8nResponse('mute.unmuted');
}
} else {
msg.il8nResponse('follow.join');
}
Expand Down Expand Up @@ -174,7 +178,7 @@ function sidle(msg) {
*
* @return {[undefined]}
* * */
function transfer(msg) {
async function transfer(msg) {
var server = msg.server;
if (server.connecting) return msg.il8nResponse('transfer.connecting');
if (server.leaving) return msg.il8nResponse('transfer.leaving');
Expand Down Expand Up @@ -218,26 +222,25 @@ function transfer(msg) {
if (server.guild.voice.connection) {
msg.il8nResponse('transfer.okay', { name: newMaster.displayName });
} else {
var errorFunc = (err) => {
var errorFunc = (err) => {};

$connection = await server.joinVoiceChannel(newMaster.voice.channel);

if (!$connection) {
msg.il8nResponse('transfer.error');
Common.error(err);
server.release();
};

server
.joinVoiceChannel(newMaster.voice.channel)
.then(() => {
msg.il8nResponse('transfer.okay', { name: newMaster.displayName });

server.addMemberSetting(newMaster, 'toLanguage', 'default');

// unmute them if they're muted
if (server.getMemberSetting(newMaster, 'muted')) {
server.addMemberSetting(newMaster, 'muted', false);
msg.il8nResponse('mute.unmuted');
}
}, errorFunc)
.catch(errorFunc);
return;
}

msg.il8nResponse('transfer.okay', { name: newMaster.displayName });

server.addMemberSetting(newMaster, 'toLanguage', 'default');

// unmute them if they're muted
if (server.getMemberSetting(newMaster, 'muted')) {
server.addMemberSetting(newMaster, 'muted', false);
msg.il8nResponse('mute.unmuted');
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/models/Command.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Command {
userJoinedChannel: self.onUserJoinedChannel || null,

messageDelivered: self.onMessageDelivered || null,
preValidate: self.onPreValidate || null,
validate: self.onValidate || null,

joinVoice: self.onJoinVoice || null,
Expand Down
104 changes: 54 additions & 50 deletions src/models/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ class Server {

// get the server to join a voice channel
// NOTE: this is async, so if you want to run a continuation use .then on the promise returned
joinVoiceChannel(voiceChannel) {
async joinVoiceChannel(voiceChannel) {
var server = this;
if (server.connecting)
return Common.error('joinVoiceChannel(' + voiceChannel.id + '): tried to connect twice!');
Expand All @@ -244,61 +244,58 @@ class Server {
);
server.connecting = true;

// join the voice channel and setup all the listeners to deal with events
var p = voiceChannel.join().then(
(connection) => {
// success

// when closing stop the voices and clear the neglect timeout
connection.on('closing', () => {
server.leaving = true;
server.stop('voiceClosing'); // stop playing
clearTimeout(server.neglect_timeout);
});

// when disconnect clear the master - note that d/c may happen without a closing event
connection.on('disconnect', () => {
server.stop('disconnect'); // stop playing
server.bound_to = null;
server.permitted = {};
server.leaving = false;
});

// if an error occurs treat it like a d/c but capture the error
// reset the state to as if there was no connection
connection.on('error', (error) => {
server.bound_to = null;
server.permitted = {};
server.leaving = false;
server.connecting = false; // this might cause a race condition
Common.error(error);
connection.disconnect(); // nerf the connection because we got an error
});

server.connecting = false;
server.save();
server.world.setPresence();
commands.notify('joinVoice', { server: server });
},
(error) => {
// on an error treat it like a error on the connection
server.stop('joinError');
server.bound_to = null;
server.permitted = {};
server.connecting = false;
Common.error(error);
},
);
let connection;
try {
// join the voice channel and setup all the listeners to deal with events
connection = await voiceChannel.join();
} catch (e) {
server.stop('joinError');
server.bound_to = null;
server.permitted = {};
server.connecting = false;
Common.error(e);
return false;
}

// when closing stop the voices and clear the neglect timeout
connection.on('closing', () => {
server.leaving = true;
server.stop('voiceClosing'); // stop playing
clearTimeout(server.neglect_timeout);
});

// when disconnect clear the master - note that d/c may happen without a closing event
connection.on('disconnect', () => {
server.stop('disconnect'); // stop playing
server.bound_to = null;
server.permitted = {};
server.leaving = false;
});

return p;
// if an error occurs treat it like a d/c but capture the error
// reset the state to as if there was no connection
connection.on('error', (error) => {
server.bound_to = null;
server.permitted = {};
server.leaving = false;
server.connecting = false; // this might cause a race condition
Common.error(error);
connection.disconnect(); // nerf the connection because we got an error
});

server.connecting = false;
server.save();
server.world.setPresence();
commands.notify('joinVoice', { server: server });

return connection;
}

// switch from whatever the current voice channel is to this voice channel
switchVoiceChannel(voiceChannel) {
async switchVoiceChannel(voiceChannel) {
var server = this;
if (!voiceChannel) return Common.error(new Error('null voiceChannel passed'));
if (!server.guild.voice.connection)
return server.joinVoiceChannel(voiceChannel).then(null, Common.error).catch(Common.error);
if (!server.guild.voice.connection) return await server.joinVoiceChannel(voiceChannel);
if (voiceChannel.id == server.guild.voice.connection.channel.id)
return Common.error('voiceChannel already joined');

Expand Down Expand Up @@ -545,7 +542,14 @@ class Server {
var server = this;
var settings = server.getMemberSettings(message.member);

var ret = commands.notify('preValidate', {
message: message,
content: message.cleanContent,
server: server,
});

if (
ret === false ||
message.cleanContent.length < 1 ||
Common.isMessageExcluded(message.cleanContent) ||
!server.inChannel() ||
Expand Down

0 comments on commit b818f5a

Please sign in to comment.