Skip to content

Commit

Permalink
3.5.0-dev-0
Browse files Browse the repository at this point in the history
Refactored database code.
Returned prefixes for text command in /help command
Fixed bug when command /playfile are bypass queue limits.
Fixed bug when bot leaving guild, the guild settings are not deleted.
  • Loading branch information
AlexInCube committed Aug 4, 2024
1 parent c4cbd2a commit 0b8e630
Show file tree
Hide file tree
Showing 26 changed files with 143 additions and 140 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aicbot",
"version": "3.4.0",
"version": "3.5.0-dev",
"description": "Discord Bot for playing music",
"main": "build/main.js",
"scripts": {
Expand Down
File renamed without changes.
10 changes: 10 additions & 0 deletions src/audioplayer/util/queueSongsIsFull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client, Guild } from 'discord.js';
import { ENV } from '../../EnvironmentVariables.js';

export function queueSongsIsFull(client: Client, guild: Guild): boolean {
const queue = client.audioPlayer.distube.getQueue(guild);

if (!queue) return false;

return queue.songs.length >= ENV.BOT_MAX_SONGS_IN_QUEUE;
}
4 changes: 2 additions & 2 deletions src/commands/admin/setPrefix.command.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { CommandArgument, ICommand } from '../../CommandTypes.js';
import { Guild, Message, PermissionsBitField, SlashCommandBuilder } from 'discord.js';
import { setGuildOption } from '../../handlers/MongoSchemas/SchemaGuild.js';
import { GroupAdmin } from './AdminTypes.js';
import i18next from 'i18next';
import { setGuildPrefix } from '../../schemas/SchemaGuild.js';

export default function (): ICommand {
return {
Expand Down Expand Up @@ -51,6 +51,6 @@ async function changePrefixTo(guild: Guild, prefix: string): Promise<string> {
if (prefix === '/' || prefix === '@' || prefix === '#')
return i18next.t('commands:set_prefix_restrict_prefixes', { prefixes: '/ @ #' }) as string;
if (prefix.length > 2) return i18next.t('commands:set_prefix_length_error') as string;
await setGuildOption(guild, 'prefix', prefix);
await setGuildPrefix(guild, prefix);
return i18next.t('commands:set_prefix_success_change', { prefix: prefix }) as string;
}
14 changes: 3 additions & 11 deletions src/commands/audio/play.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { CommandArgument, ICommand } from '../../CommandTypes.js';
import {
ApplicationCommandOptionChoiceData,
AutocompleteInteraction,
Client,
Guild,
GuildMember,
Message,
Expand All @@ -19,6 +18,7 @@ import { SearchResultType } from '@distube/youtube';
import ytsr from '@distube/ytsr';
import { generateWarningEmbed } from '../../utilities/generateWarningEmbed.js';
import { ENV } from '../../EnvironmentVariables.js';
import { queueSongsIsFull } from '../../audioplayer/util/queueSongsIsFull.js';

export const services = 'Youtube, Spotify, Soundcloud, Yandex Music, Apple Music, HTTP-stream';
export default function (): ICommand {
Expand Down Expand Up @@ -76,8 +76,6 @@ export default function (): ICommand {
),
autocomplete: songSearchAutocomplete,
execute: async (interaction) => {
const songQuery = interaction.options.getString('request');

if (queueSongsIsFull(interaction.client, interaction.guild as Guild)) {
await interaction.reply({
embeds: [
Expand All @@ -92,6 +90,8 @@ export default function (): ICommand {
return;
}

const songQuery = interaction.options.getString('request');

await interaction.reply({
content: i18next.t('general:thinking') as string
});
Expand Down Expand Up @@ -156,11 +156,3 @@ export async function songSearchAutocomplete(interaction: AutocompleteInteractio

await interaction.respond([]);
}

function queueSongsIsFull(client: Client, guild: Guild): boolean {
const queue = client.audioPlayer.distube.getQueue(guild);

if (!queue) return false;

return queue.songs.length >= ENV.BOT_MAX_SONGS_IN_QUEUE;
}
31 changes: 31 additions & 0 deletions src/commands/audio/playfile.command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ICommand } from '../../CommandTypes.js';
import {
Guild,
GuildMember,
Message,
PermissionsBitField,
Expand All @@ -11,6 +12,9 @@ import { GroupAudio } from './AudioTypes.js';
import { isAudioFile } from '../../audioplayer/util/isAudioFile.js';
import { generateErrorEmbed } from '../../utilities/generateErrorEmbed.js';
import i18next from 'i18next';
import { queueSongsIsFull } from '../../audioplayer/util/queueSongsIsFull.js';
import { generateWarningEmbed } from '../../utilities/generateWarningEmbed.js';
import { ENV } from '../../EnvironmentVariables.js';

const audioFormats = { formats: 'mp3/wav/ogg' };

Expand All @@ -20,6 +24,19 @@ export default function (): ICommand {
name: 'playfile',
description: i18next.t('commands:play_file_desc'),
execute: async (message: Message) => {
if (queueSongsIsFull(message.client, message.guild as Guild)) {
await message.reply({
embeds: [
generateWarningEmbed(
i18next.t('commands:play_error_songs_limit', {
queueLimit: ENV.BOT_MAX_SONGS_IN_QUEUE
}) as string
)
]
});
return;
}

const musicFile = message.attachments.first();

if (!musicFile) {
Expand Down Expand Up @@ -63,6 +80,20 @@ export default function (): ICommand {
.setRequired(true)
),
execute: async (interaction) => {
if (queueSongsIsFull(interaction.client, interaction.guild as Guild)) {
await interaction.reply({
embeds: [
generateWarningEmbed(
i18next.t('commands:play_error_songs_limit', {
queueLimit: ENV.BOT_MAX_SONGS_IN_QUEUE
}) as string
)
],
ephemeral: true
});
return;
}

const musicFile = interaction.options.getAttachment('file', true);

if (!isAudioFile(musicFile.name)) {
Expand Down
23 changes: 17 additions & 6 deletions src/commands/info/help.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
PermissionsBitField,
SlashCommandBuilder
} from 'discord.js';
import '../../Types.js';
import '../../DiscordTypes.js';
import { GroupInfo } from './InfoTypes.js';
import i18next from 'i18next';
import { ENV } from '../../EnvironmentVariables.js';
import { getGuildPrefix } from '../../schemas/SchemaGuild.js';

export default function (): ICommand {
return {
Expand Down Expand Up @@ -42,7 +44,7 @@ export default function (): ICommand {
} else {
// Если указана конкретная команда
await message.reply({
embeds: [await generateCommandsEmbedList(message.client)],
embeds: [await generateCommandsEmbedList(message.client, message.guild)],
allowedMentions: { users: [] }
});
}
Expand Down Expand Up @@ -81,7 +83,7 @@ export default function (): ICommand {
});
} else {
await interaction.reply({
embeds: [await generateCommandsEmbedList(interaction.client)],
embeds: [await generateCommandsEmbedList(interaction.client, interaction.guild)],
ephemeral: true
});
}
Expand Down Expand Up @@ -188,11 +190,20 @@ export function generateSpecificCommandHelp(
return helpEmbed;
}

export async function generateCommandsEmbedList(client: Client): Promise<EmbedBuilder> {
export async function generateCommandsEmbedList(
client: Client,
guild: Guild | null
): Promise<EmbedBuilder> {
let guildPrefix: string | undefined = undefined;
if (guild) guildPrefix = await getGuildPrefix(guild);

const helpEmbed = new EmbedBuilder()
.setColor('#436df7')
.setTitle(i18next.t('commands:help_about_commands'))
.setDescription(`Bot github: https://github.com/AlexInCube/AlCoTest`);
.setTitle(i18next.t('commands:help_about_commands')).setDescription(`
${i18next.t('commands:help_embed_description', { prefix: ENV.BOT_COMMAND_PREFIX, interpolation: { escapeValue: false } })} ${guildPrefix ? i18next.t('commands:help_embed_description_server_prefix', { prefix: guildPrefix, interpolation: { escapeValue: false } }) : ''}
\n
GitHub: https://github.com/AlexInCube/AlCoTest
`);

client.commandsGroups.forEach((group) => {
let commandsList = '';
Expand Down
2 changes: 1 addition & 1 deletion src/events/interactionCreate.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { slashCommandHandler } from './interactionHandlers/slashCommandHandler.js';
import { autocompleteHandler } from './interactionHandlers/autocompleteHandler.js';
import { Client, Events, Interaction } from 'discord.js';
Expand Down
2 changes: 1 addition & 1 deletion src/events/messageCreate.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Events, Message, TextChannel } from 'discord.js';
import { textCommandsHandler } from './messageHandlers/textCommandsHandler.js';
import { AudioPlayerEventMessageCreate } from '../audioplayer/eventsHandlers/AudioPlayerEventMessageCreate.js';
Expand Down
2 changes: 1 addition & 1 deletion src/events/messageDeleted.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Events, Message } from 'discord.js';
import { AudioPlayerEventMessageDeleted } from '../audioplayer/eventsHandlers/AudioPlayerEventMessageDeleted.js';

Expand Down
9 changes: 3 additions & 6 deletions src/events/messageHandlers/textCommandsHandler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { MongoCheckConnection } from '../../handlers/Mongo.handler.js';
import { getGuildOption } from '../../handlers/MongoSchemas/SchemaGuild.js';
import { ICommand } from '../../CommandTypes.js';
import { generateSpecificCommandHelp } from '../../commands/info/help.command.js';
import { generateErrorEmbed } from '../../utilities/generateErrorEmbed.js';
Expand All @@ -12,6 +10,7 @@ import { loggerError } from '../../utilities/logger.js';
import i18next from 'i18next';
import { loggerPrefixCommandHandler } from '../../handlers/Command.handler.js';
import { ENV } from '../../EnvironmentVariables.js';
import { getGuildPrefix } from '../../schemas/SchemaGuild.js';

export async function textCommandsHandler(client: Client, message: Message) {
try {
Expand All @@ -21,10 +20,8 @@ export async function textCommandsHandler(client: Client, message: Message) {

if (!message.content.startsWith(ENV.BOT_COMMAND_PREFIX)) {
if (message.guild) {
if (MongoCheckConnection()) {
const guildPrefix = await getGuildOption(message.guild, 'prefix');
if (guildPrefix) prefix = guildPrefix;
}
const guildPrefix = await getGuildPrefix(message.guild);
if (guildPrefix) prefix = guildPrefix;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/events/onChannelDelete.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Events, TextChannel } from 'discord.js';
import { AudioPlayerEventChannelDelete } from '../audioplayer/eventsHandlers/AudioPlayerEventChannelDelete.js';

Expand Down
6 changes: 3 additions & 3 deletions src/events/onGuildCreate.event.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Guild } from 'discord.js';
import { setupSettings } from '../handlers/MongoSchemas/SchemaGuild.js';
import { setupGuildSettings } from '../schemas/SchemaGuild.js';
import { Events } from 'discord.js';

const event: BotEvent = {
name: Events.GuildCreate,
execute: async (client: Client, guild: Guild) => {
await setupSettings(guild);
await setupGuildSettings(guild);
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/events/onGuildDelete.event.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Guild } from 'discord.js';
import { setupSettings } from '../handlers/MongoSchemas/SchemaGuild.js';
import { deleteGuildSettings } from '../schemas/SchemaGuild.js';
import { Events } from 'discord.js';

const event: BotEvent = {
name: Events.GuildDelete,
execute: async (client: Client, guild: Guild) => {
await setupSettings(guild);
await deleteGuildSettings(guild);
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/events/onReady.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { loggerSend } from '../utilities/logger.js';
import { Events } from 'discord.js';

Expand Down
2 changes: 1 addition & 1 deletion src/events/voiceChannelUpdate.event.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BotEvent } from '../Types';
import { BotEvent } from '../DiscordTypes.js';
import { Client, Events, VoiceState } from 'discord.js';
import { AudioPlayerEventVoiceChannelUpdate } from '../audioplayer/eventsHandlers/AudioPlayerEventVoiceChannelUpdate.js';

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/Command.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { loggerError, loggerSend, loggerWarn } from '../utilities/logger.js';
import { ICommand, ICommandGroup, SlashBuilder } from '../CommandTypes.js';
import * as fs from 'fs';
import * as path from 'path';
import '../Types.js';
import '../DiscordTypes.js';
import getDirName from '../utilities/getDirName.js';
import { ENV } from '../EnvironmentVariables.js';

Expand Down
2 changes: 1 addition & 1 deletion src/handlers/Event.handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Client } from 'discord.js';
import { BotEvent } from '../Types.js';
import { BotEvent } from '../DiscordTypes.js';
import { loggerSend } from '../utilities/logger.js';
import getDirName from '../utilities/getDirName.js';
import fs from 'node:fs/promises';
Expand Down
10 changes: 1 addition & 9 deletions src/handlers/Mongo.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export const loggerPrefixMongo = 'MongoDB';

export default async function mongoHandler() {
const MONGO_URI = ENV.MONGO_URI;
mongoose.set('strictQuery', true);
mongoose.set('strictQuery', 'throw');
mongoose.pluralize(null);

try {
Expand All @@ -17,11 +17,3 @@ export default async function mongoHandler() {
throw error;
}
}

export function MongoCheckConnection() {
if (mongoose.connection.readyState === 0) {
loggerSend('Connection is not established', loggerPrefixMongo);
return false;
}
return true;
}
59 changes: 0 additions & 59 deletions src/handlers/MongoSchemas/SchemaGuild.ts

This file was deleted.

Loading

0 comments on commit 0b8e630

Please sign in to comment.