Skip to content

Commit

Permalink
feat: i18n (#42)
Browse files Browse the repository at this point in the history
* chore: config files for sapphire/i18n

* feat: i18n for misc commands

* fix: cancelgame change file name

* feat: redefinition of locales getters with t function, games commands i18n

* fix: use of args.t for i18n in misc commands

* feat: i18n for roles related commands

* feat: i18n for listeners and preconditions

* refactor: preconditions names

* feat: i18n for utils

* fix: replace Users for username strings in i18n texts

* feat: selectLanguage command

* refactor: configuration keys

* feat: selectLanguage command

* feat: spanish translations for listeners, preconditions, categories, errors and roleAssignment messages

* feat: spanish i18n for commands

* fix: selectlanguages's ignore state

* chore: version 2.4.0
  • Loading branch information
IgnacioNMiranda authored Dec 30, 2021
1 parent 230d906 commit 2b4a120
Show file tree
Hide file tree
Showing 109 changed files with 1,801 additions and 1,289 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"singleQuote": true,
"semi": false
"semi": false,
"printWidth": 120
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "chibi-knight",
"version": "2.3.0",
"version": "2.4.0",
"description": "A multipurpose discord bot",
"main": "dist/app.js",
"scripts": {
"start": "node dist/app.js",
"dev": "tsc-watch --onSuccess \"sh -c 'tsc-alias && node dist/app.js'\"",
"build": "rm -rf dist && tsc && tsc-alias",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint \"{src, test}/**/*.{js,ts}\" --fix",
"lint": "eslint \"{src,test}/**/*.{js,ts}\" --fix",
"prettier": "prettier --write \"{src,test}/**/*.{js,ts}\"",
"prepare": "husky install"
},
"repository": {
Expand All @@ -27,6 +28,7 @@
"private": "true",
"dependencies": {
"@sapphire/framework": "^2.2.2",
"@sapphire/plugin-i18next": "^2.1.4",
"@sapphire/plugin-logger": "^2.1.1",
"discord.js": "^13.4.0",
"dotenv": "^10.0.0",
Expand Down
80 changes: 35 additions & 45 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,35 @@
import { configuration } from '@/config'
import { container, SapphireClient } from '@sapphire/framework'
import { Cache, MongoDatabase } from '@/database'
import { logger } from '@/utils'
import '@sapphire/plugin-logger/register'

const main = async () => {
const client = new SapphireClient({
defaultPrefix: configuration.prefix,
intents: ['GUILDS', 'GUILD_MEMBERS', 'GUILD_MESSAGES'],
loadDefaultErrorListeners: false,
})

logger.info(`Initializing application...`, {
context: client.constructor.name,
})

logger.info(`Trying to connect to mongo database...`, {
context: client.constructor.name,
})
try {
container.db = await MongoDatabase.connect()
container.cache = await Cache.init()
} catch (error) {
logger.error(`Database connection error. Could not connect to databases`, {
context: client.constructor.name,
})
}

try {
logger.info('Logging in...', {
context: client.constructor.name,
})
await client.login(configuration.token)
} catch (error) {
const { code, method, path } = error
console.error(`Error ${code} trying to ${method} to ${path} path`)
}
}

main().catch(() =>
logger.error('Bot initialization failed', {
context: container.client.constructor.name,
})
)
import { configuration } from '@/config'
import { container, SapphireClient } from '@sapphire/framework'
import { Cache, MongoDatabase } from '@/database'
import { i18nConfig, logger } from '@/utils'
import '@sapphire/plugin-logger/register'
import '@sapphire/plugin-i18next/register'

const main = async () => {
const client = new SapphireClient({
...configuration.client,
intents: ['GUILDS', 'GUILD_MEMBERS', 'GUILD_MESSAGES'],
i18n: i18nConfig,
})

logger.info(`Initializing application...`, {
context: client.constructor.name,
})

logger.info(`Trying to connect to mongo database...`, {
context: client.constructor.name,
})
container.db = await MongoDatabase.connect()
container.cache = await Cache.init()

logger.info('Logging in...', {
context: client.constructor.name,
})
await client.login(configuration.client.token)
}

main().catch((reason) => {
logger.error(`Bot initialization failed. Error: ${reason}`, {
context: container.client.constructor.name,
})
})
46 changes: 0 additions & 46 deletions src/commands/games/cancelGame.ts

This file was deleted.

43 changes: 43 additions & 0 deletions src/commands/games/cancelgame.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { Command, CommandOptionsRunTypeEnum, container } from '@sapphire/framework'
import type { Message } from 'discord.js'
import { logger, CustomPrecondition, languageKeys, CustomCommand, CustomArgs } from '@/utils'

/**
* Replies the receives message on command.
*/
export class CancelGameCommand extends CustomCommand {
constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
aliases: ['cg'],
description: languageKeys.commands.games.cancelgame.description,
preconditions: [CustomPrecondition.BotInitializedOnly],
runIn: [CommandOptionsRunTypeEnum.GuildAny],
})
}

/**
* It executes when someone types the "say" command.
*/
async messageRun(message: Message, { t }: CustomArgs): Promise<Message<boolean>> {
const { id } = message.guild

try {
const guild = await container.db.guildService.getById(id)

if (guild && !guild.gameInstanceActive) {
return message.channel.send(t(languageKeys.commands.games.cancelgame.noActiveGame))
}

guild.gameInstanceActive = false
await guild.save()
return message.channel.send(t(languageKeys.commands.games.cancelgame.gameCancelled))
} catch (error) {
logger.error(
`(${this.constructor.name}): MongoDB Connection error. Could not change game instance state for '${message.guild.name}' server`
)
}

return message.channel.send(t(languageKeys.errors.unexpectedError))
}
}
Loading

0 comments on commit 2b4a120

Please sign in to comment.