Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add commands for enabling/disabling voice commands and automatic joining #34

Merged
merged 26 commits into from
Sep 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
ba031ea
Add EnableCommand to enable voice commands
sweep-ai[bot] Sep 2, 2023
f4b7f2d
Add DisableCommand to disable voice commands
sweep-ai[bot] Sep 2, 2023
1994d1c
Added EnableAutoJoinCommand to enable automatic jo
sweep-ai[bot] Sep 2, 2023
a55ff7a
Add DisableAutoJoinCommand to disable automatic jo
sweep-ai[bot] Sep 2, 2023
f7168b3
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
836f299
Moved voice command phrases to separate file
sweep-ai[bot] Sep 2, 2023
ea791a9
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
543bdf8
Updated src_modules/voice_commands/phrases.ts
sweep-ai[bot] Sep 2, 2023
e14c2ed
Updated src_modules/voice_commands/phrases.ts
sweep-ai[bot] Sep 2, 2023
936ec0b
Updated src_modules/voice_commands/commands/Disabl
sweep-ai[bot] Sep 2, 2023
551e87e
Updated src_modules/voice_commands/commands/Disabl
sweep-ai[bot] Sep 2, 2023
b73a492
Updated src_modules/voice_commands/commands/Enable
sweep-ai[bot] Sep 2, 2023
f1fd0ed
Updated src_modules/voice_commands/commands/Enable
sweep-ai[bot] Sep 2, 2023
8a86de5
Updated src_modules/voice_commands/phrases.ts
sweep-ai[bot] Sep 2, 2023
f12376a
Updated src_modules/voice_commands/phrases.ts
sweep-ai[bot] Sep 2, 2023
ab11a50
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
26b528a
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
86317d6
Updated src_modules/voice_commands/phrases.ts
sweep-ai[bot] Sep 2, 2023
bd3722a
Updated src_modules/voice_commands/commands/Enable
sweep-ai[bot] Sep 2, 2023
7bea65e
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
38427e3
Updated src_modules/voice_commands/index.ts
sweep-ai[bot] Sep 2, 2023
96cdc09
Updated src_modules/voice_commands/commands/Disabl
sweep-ai[bot] Sep 2, 2023
127ed6b
Updated src_modules/voice_commands/commands/Disabl
sweep-ai[bot] Sep 2, 2023
8ce9294
Updated src_modules/voice_commands/commands/Enable
sweep-ai[bot] Sep 2, 2023
1d1fd3d
Updated src_modules/voice_commands/commands/Enable
sweep-ai[bot] Sep 2, 2023
2f19768
Fix implementation
lasa01 Sep 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src_modules/voice_commands/commands/DisableAutoJoinCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command, IExecutionContext } from "../../..";
import VoiceCommandsModule from "..";
import { autoJoinDisabledPhrase, autoJoinAlreadyDisabledPhrase } from "../phrases";

export class DisableAutoJoinCommand extends Command<[]> {
private voiceCommandsModule: VoiceCommandsModule;

public constructor(voiceCommandsModule: VoiceCommandsModule) {
super(
{
allowedPrivileges: ["admin"],
author: "extcord",
description: "Disable automatic joining",
globalAliases: ["disableautojoin"],
name: "disableautojoin",
},
[],
);

this.voiceCommandsModule = voiceCommandsModule;
}

public async execute(context: IExecutionContext<[]>) {
const guild = context.guild;

if (await this.voiceCommandsModule.autoJoinEnabledConfigEntry.guildGet(guild)) {
await this.voiceCommandsModule.autoJoinEnabledConfigEntry.guildSet(guild, false);
return context.respond(autoJoinDisabledPhrase, {});
} else {
return context.respond(autoJoinAlreadyDisabledPhrase, {});
}
}
}
33 changes: 33 additions & 0 deletions src_modules/voice_commands/commands/DisableCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command, IExecutionContext } from "../../..";
import VoiceCommandsModule from "..";
import { voiceCommandsDisabledPhrase, voiceCommandsAlreadyDisabledPhrase } from "../phrases";

export class DisableCommand extends Command<[]> {
private voiceCommandsModule: VoiceCommandsModule;

public constructor(voiceCommandsModule: VoiceCommandsModule) {
super(
{
allowedPrivileges: ["admin"],
author: "extcord",
description: "Disable voice commands",
globalAliases: [],
name: "disable",
},
[],
);

this.voiceCommandsModule = voiceCommandsModule;
}

public async execute(context: IExecutionContext<[]>) {
const guild = context.guild;

if (await this.voiceCommandsModule.voiceCommandsEnabledConfigEntry.guildGet(guild)) {
await this.voiceCommandsModule.voiceCommandsEnabledConfigEntry.guildSet(guild, false);
return context.respond(voiceCommandsDisabledPhrase, {});
} else {
return context.respond(voiceCommandsAlreadyDisabledPhrase, {});
}
}
}
33 changes: 33 additions & 0 deletions src_modules/voice_commands/commands/EnableAutoJoinCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Command, IExecutionContext } from "../../..";
import VoiceCommandsModule from "..";
import { autoJoinEnabledPhrase, autoJoinAlreadyEnabledPhrase } from "../phrases";

export class EnableAutoJoinCommand extends Command<[]> {
private voiceCommandsModule: VoiceCommandsModule;

public constructor(voiceCommandsModule: VoiceCommandsModule) {
super(
{
allowedPrivileges: ["admin"],
author: "extcord",
description: "Enable automatic joining",
globalAliases: ["enableautojoin"],
name: "enableautojoin",
},
[],
);

this.voiceCommandsModule = voiceCommandsModule;
}

public async execute(context: IExecutionContext<[]>) {
const guild = context.guild;

if (await this.voiceCommandsModule.autoJoinEnabledConfigEntry.guildGet(guild)) {
return context.respond(autoJoinAlreadyEnabledPhrase, {});
}

await this.voiceCommandsModule.autoJoinEnabledConfigEntry.guildSet(guild, true);
return context.respond(autoJoinEnabledPhrase, {});
}
}
39 changes: 39 additions & 0 deletions src_modules/voice_commands/commands/EnableCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Command, IExecutionContext } from "../../..";
import VoiceCommandsModule from "..";
import { voiceCommandsAlreadyEnabledPhrase, voiceCommandsEnabledPhrase, voiceCommandsNotSupportedPhrase } from "../phrases";

export class EnableCommand extends Command<[]> {
private voiceCommandsModule: VoiceCommandsModule;

public constructor(voiceCommandsModule: VoiceCommandsModule) {
super(
{
allowedPrivileges: ["admin"],
author: "extcord",
description: "Enable voice commands",
globalAliases: [],
name: "enable",
},
[],
);

this.voiceCommandsModule = voiceCommandsModule;
}

public async execute(context: IExecutionContext<[]>) {
const guild = context.guild;

if (await this.voiceCommandsModule.voiceCommandsEnabledConfigEntry.guildGet(guild)) {
return context.respond(voiceCommandsAlreadyEnabledPhrase, {});
}

const backendLanguage = this.voiceCommandsModule.backendLanguageIdPhrase.get(context.language);

if (backendLanguage === undefined || backendLanguage === "") {
return context.respond(voiceCommandsNotSupportedPhrase, {});;
}

await this.voiceCommandsModule.voiceCommandsEnabledConfigEntry.guildSet(guild, true);
return context.respond(voiceCommandsEnabledPhrase, {});
}
}
70 changes: 60 additions & 10 deletions src_modules/voice_commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@
import { GatewayIntentBits, Guild, VoiceState, VoiceChannel } from "discord.js";
import { VoiceConnection } from "@discordjs/voice";

import { BooleanGuildConfigEntry, Bot, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../..";
import { BooleanGuildConfigEntry, Bot, CommandGroup, Logger, Module, SimplePhrase, StringConfigEntry, NumberConfigEntry } from "../..";
import { voiceCommandsEnabledPhrase, voiceCommandsDisabledPhrase, autoJoinEnabledPhrase, autoJoinDisabledPhrase, voiceCommandsAlreadyDisabledPhrase, voiceCommandsAlreadyEnabledPhrase, autoJoinAlreadyDisabledPhrase, autoJoinAlreadyEnabledPhrase, voiceCommandsNotSupportedPhrase } from "./phrases";

import { GuildListener } from "./GuildListener";
import { VoiceBackendClient } from "./VoiceBackendClient";
import { PhoneticCache } from "./PhoneticCache";
import { SpeechCache } from "./SpeechCache";
import { VoiceCommands } from "./VoiceCommands";
import { DisableCommand } from "./commands/DisableCommand";
import { EnableCommand } from "./commands/EnableCommand";
import { EnableAutoJoinCommand } from "./commands/EnableAutoJoinCommand";
import { DisableAutoJoinCommand } from "./commands/DisableAutoJoinCommand";

export default class VoiceCommandsModule extends Module {
private listeners: Map<string, GuildListener>;
Expand All @@ -25,8 +30,10 @@ export default class VoiceCommandsModule extends Module {
public maxQueuedAsrCountConfigEntry: NumberConfigEntry;
public tokenConfigEntry: StringConfigEntry;
public voiceCommandsEnabledConfigEntry: BooleanGuildConfigEntry;
public autoJoinEnabledConfigEntry: BooleanGuildConfigEntry;
public client: VoiceBackendClient;
public voiceCommands: VoiceCommands;
public voiceCommandsGroup: CommandGroup;
public phoneticCache: PhoneticCache;
public speechCache: SpeechCache;

Expand Down Expand Up @@ -97,13 +104,44 @@ export default class VoiceCommandsModule extends Module {
}, bot.database, true);
this.registerConfigEntry(this.voiceCommandsEnabledConfigEntry);

this.autoJoinEnabledConfigEntry = new BooleanGuildConfigEntry({
name: "autoJoinEnabled",
}, bot.database, false);
this.registerConfigEntry(this.autoJoinEnabledConfigEntry);

bot.intents.push(GatewayIntentBits.GuildVoiceStates);

this.client = new VoiceBackendClient(this);
this.voiceCommands = new VoiceCommands(this, bot);
this.phoneticCache = new PhoneticCache(this, bot);
this.speechCache = new SpeechCache(this, bot);

this.voiceCommandsGroup = new CommandGroup(
{
allowedPrivileges: ["everyone"],
author: "extcord",
description: "Commands for managing voice commands and automatic joining",
name: "voice-commands",
},
);
this.voiceCommandsGroup.addSubcommands(
new EnableCommand(this),
new DisableCommand(this),
new EnableAutoJoinCommand(this),
new DisableAutoJoinCommand(this),
);
this.voiceCommandsGroup.addPhrases(
voiceCommandsEnabledPhrase,
voiceCommandsDisabledPhrase,
autoJoinEnabledPhrase,
autoJoinDisabledPhrase,
voiceCommandsAlreadyDisabledPhrase,
voiceCommandsAlreadyEnabledPhrase,
autoJoinAlreadyDisabledPhrase,
autoJoinAlreadyEnabledPhrase,
voiceCommandsNotSupportedPhrase,
);
this.registerCommand(this.voiceCommandsGroup);
}

public async getListener(guild: Guild): Promise<GuildListener | undefined> {
Expand Down Expand Up @@ -143,15 +181,27 @@ export default class VoiceCommandsModule extends Module {
if (oldState.channel === null && newState.channel !== null && newState.channel instanceof VoiceChannel && !newState.member?.user.bot) {
const channel = newState.channel;

// Someone joined a voice channel
const nonBotUsers = channel.members.filter(member => !member.user.bot);
if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) {
setTimeout(async () => {
const nonBotUsers = channel.members.filter(member => !member.user.bot);
if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) {
await this.getConnection(this.bot, channel);
}
}, 5000);
this.bot.database.ensureConnection();

const guildEntity = await this.bot.database.repos.guild.getEntity(newState.guild);

const extendedGuild = {
entity: guildEntity,
guild: newState.guild,
};

const autoJoinEnabled = await this.autoJoinEnabledConfigEntry.guildGet(extendedGuild);

if (autoJoinEnabled) {
const nonBotUsers = channel.members.filter(member => !member.user.bot);
if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) {
setTimeout(async () => {
const nonBotUsers = channel.members.filter(member => !member.user.bot);
if (nonBotUsers.size > 0 && this.bot.voice.getConnection(channel.guild) === undefined) {
await this.getConnection(this.bot, channel);
}
}, 5000);
}
}
}

Expand Down
118 changes: 118 additions & 0 deletions src_modules/voice_commands/phrases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import { MessagePhrase } from "../..";

export const voiceCommandsEnabledPhrase = new MessagePhrase(
{
description: "Shown when voice commands are enabled",
name: "voiceCommandsEnabled",
},
"Voice commands enabled.",
{
timestamp: false,
title: "Voice commands enabled."
},
{},
);

export const voiceCommandsDisabledPhrase = new MessagePhrase(
{
description: "Shown when voice commands are disabled",
name: "voiceCommandsDisabled",
},
"Voice commands disabled.",
{
timestamp: false,
title: "Voice commands disabled."
},
{},
);

export const autoJoinEnabledPhrase = new MessagePhrase(
{
description: "Shown when automatic joining is enabled",
name: "autoJoinEnabled",
},
"Automatic joining enabled.",
{
timestamp: false,
title: "Automatic joining enabled."
},
{},
);

export const autoJoinDisabledPhrase = new MessagePhrase(
{
description: "Shown when automatic joining is disabled",
name: "autoJoinDisabled",
},
"Automatic joining disabled.",
{
timestamp: false,
title: "Automatic joining disabled."
},
{},
);

export const autoJoinAlreadyDisabledPhrase = new MessagePhrase(
{
description: "Shown when automatic joining is already disabled",
name: "autoJoinAlreadyDisabled",
},
"Automatic joining is already disabled.",
{
timestamp: false,
title: "Automatic joining already disabled."
},
{},
);

export const voiceCommandsAlreadyDisabledPhrase = new MessagePhrase(
{
description: "Shown when voice commands are already disabled",
name: "voiceCommandsAlreadyDisabled",
},
"Voice commands are already disabled.",
{
timestamp: false,
title: "Voice commands already disabled."
},
{},
);

export const autoJoinAlreadyEnabledPhrase = new MessagePhrase(
{
description: "Shown when automatic joining is already enabled",
name: "autoJoinAlreadyEnabled",
},
"Automatic joining is already enabled.",
{
timestamp: false,
title: "Automatic joining already enabled."
},
{},
);

export const voiceCommandsAlreadyEnabledPhrase = new MessagePhrase(
{
description: "Shown when voice commands are already enabled",
name: "voiceCommandsAlreadyEnabled",
},
"Voice commands are already enabled.",
{
timestamp: false,
title: "Voice commands already enabled."
},
{},
);

export const voiceCommandsNotSupportedPhrase = new MessagePhrase(
{
description: "Shown when voice commands are not supported with the language",
name: "voiceCommandsNotSupported",
},
"Voice commands are not supported with this language.",
{
timestamp: false,
title: "Voice commands are not supported with this language."
},
{},
);