Skip to content

Commit

Permalink
Merge pull request #119 from Chr1s70ph/i18n
Browse files Browse the repository at this point in the history
Add support for multiple languages (en/de)
What could go wrong?
  • Loading branch information
Chr1s70ph authored Jan 3, 2022
2 parents 75ad16d + 68f8364 commit ece3241
Show file tree
Hide file tree
Showing 37 changed files with 1,149 additions and 588 deletions.
14 changes: 9 additions & 5 deletions commands/admin/restart.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'

/**
* Const required, otherwise pm2 throws an error.
*/
Expand All @@ -11,16 +11,20 @@ exports.description = ''

exports.usage = 'restart'

exports.run = (client: DiscordClient, message: Message) => {
exports.run = (client: DiscordClient, message: DiscordMessage) => {
/**
* Check if user has the correct rights to execute the command.
*/
if (!Object.values(client.config.ids.acceptedAdmins).includes(message.author.id)) {
return client.reply(message, { content: 'You do not have the permissions to perform that command.' })
return client.reply(message, {
content: client.translate({ key: 'commands.admin.missingPermission', lng: message.author.language }),
})
}

pm2Handle()
return client.send(message, { content: '🤖Restarting...' })
return client.send(message, {
content: client.translate({ key: 'commands.admin.restart', lng: message.author.language }),
})
}

/**
Expand Down
14 changes: 8 additions & 6 deletions commands/admin/say.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { MessageEmbed, Message } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { MessageEmbed } from 'discord.js'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'

exports.name = 'say'

exports.description = 'Der Bot sagt, was man ihm sagt, dass er sagen soll, weil er dir nach sagt.'

exports.usage = 'say <messageContent>'

exports.run = (client: DiscordClient, message: Message) => {
exports.run = (client: DiscordClient, message: DiscordMessage) => {
/**
* Check if user has the correct rights to execute the command.
*/
if (!Object.values(client.config.ids.acceptedAdmins).includes(message.author.id)) {
return client.reply(message, { content: 'You do not have the permissions to perform that command.' })
return client.reply(message, {
content: client.translate({ key: 'commands.admin.missingPermission', lng: message.author.language }),
})
}

/**
Expand All @@ -30,11 +32,11 @@ exports.run = (client: DiscordClient, message: Message) => {

/**
*
* @param {Message} message command Message
* @param {DiscordMessage} message command Message
* @param {DiscordClient} client Bot-Client
* @returns {MessageEmbed} embed with given message.content
*/
function createEmbed(message: Message<boolean>, client: DiscordClient): MessageEmbed {
function createEmbed(message: DiscordMessage, client: DiscordClient): MessageEmbed {
const messageContent = message.content.substring(message.content.indexOf(' ') + client.config.prefix.length)

const embed = new MessageEmbed()
Expand Down
24 changes: 16 additions & 8 deletions commands/admin/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Message, PresenceData as setPresenceData, PresenceStatusData } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'
exports.name = 'status'

exports.description = 'Setzt den Status des Bottes'
Expand All @@ -24,20 +24,22 @@ const presence: setPresenceData = {

exports.presence = presence

exports.run = (client: DiscordClient, message: Message) => {
exports.run = (client: DiscordClient, message: DiscordMessage) => {
/**
* Check if user has the correct rights to execute the command.
*/
if (!Object.values(client.config.ids.acceptedAdmins).includes(message.author.id)) {
return client.reply(message, { content: 'You do not have the permissions to perform that command.' })
return client.reply(message, {
content: client.translate({ key: 'commands.admin.missingPermission', lng: message.author.language }),
})
}

/**
* Content of message.
*/
let messageContent: string = message.content

messageContent = messageContent.split('.status')[1]
messageContent = messageContent.split(`${client.config.prefix}status`)[1]

/**
* Set activity name.
Expand Down Expand Up @@ -84,12 +86,18 @@ function getPresenceStatusData(messageContent: string): PresenceStatusData {
}

/**
*
* @param {DiscordClient} client {@link DiscordClient}
* @param {Message} message Command {@link Message}
* @param {DiscordMessage} message Command {@link Message}
* @param {setPresenceData} _presence {@link setPresenceData}
* @returns {Promise<Message<boolean>>}
*/
function setPresenceData(client: DiscordClient, message: Message, _presence: setPresenceData): void {
function setPresenceData(
client: DiscordClient,
message: DiscordMessage,
_presence: setPresenceData,
): Promise<Message<boolean>> {
client.user.setPresence(_presence)
message.channel.send('👥Präsenz wurde geupdated!')
return client.send(message, {
content: client.translate({ key: 'commands.admin.status', lng: message.author.language }),
})
}
41 changes: 0 additions & 41 deletions commands/admin/stopbackupbot.ts

This file was deleted.

8 changes: 4 additions & 4 deletions commands/fun/fact.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs'
import { Message, MessageEmbed } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { MessageEmbed } from 'discord.js'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'

const FACTS_FILE = './data/facts.txt'

Expand All @@ -10,7 +10,7 @@ exports.description = 'Willst du Fakten? Dann bist du hier genau richtig.'

exports.usage = 'fact'

exports.run = (client: DiscordClient, message: Message) => {
exports.run = (client: DiscordClient, message: DiscordMessage) => {
/**
* Read data from facts file.
* @type {string}
Expand All @@ -29,7 +29,7 @@ exports.run = (client: DiscordClient, message: Message) => {
return client.send(message, {
embeds: [
new MessageEmbed()
.setTitle('🧠Fact')
.setTitle(client.translate({ key: 'commands.fun.fact', lng: message.author.language }))
.setDescription(fact)
.setFooter(message.author.tag, message.author.avatarURL({ dynamic: true })),
],
Expand Down
20 changes: 14 additions & 6 deletions commands/fun/tenor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { MessageEmbed, Message, MessageMentions, UserMention, MessageEmbedFooter } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'

exports.name = 'tenor'

Expand All @@ -10,7 +10,7 @@ exports.usage = `tenor <searchQuery>`

exports.aliases = ['gif']

exports.run = (client: DiscordClient, message: Message, args: string[]) => {
exports.run = (client: DiscordClient, message: DiscordMessage, language: string, args: string[]) => {
/**
* Get {@link UserMention} from message.
* @type {string}
Expand All @@ -26,7 +26,9 @@ exports.run = (client: DiscordClient, message: Message, args: string[]) => {
* Send reply.
*/
return searchQuery.length === 0
? client.send(message, { content: 'Bitte gebe einen suchwort an!' })
? client.send(message, {
content: client.translate({ key: 'commands.fun.tenor.ErrorMissingSearchQuery', lng: message.author.language }),
})
: queryTenorAndReply(client, searchQuery, message, userPing)
}

Expand All @@ -49,14 +51,14 @@ function removeMatching(originalArray: string[], regex: RegExp): string[] {
* Query the Tenor-API and send a reply with a random GIF.
* @param {DiscordClient} client Bot-Client.
* @param {string} searchQuery Query to search for with the tenor api
* @param {Message<boolean>} message Command-message to respond to
* @param {DiscordMessage} message Command-message to respond to
* @param {string} userPing User to mention
* @returns {any}
*/
function queryTenorAndReply(
client: DiscordClient,
searchQuery: string,
message: Message<boolean>,
message: DiscordMessage,
userPing: string,
): any {
/**
Expand All @@ -81,9 +83,15 @@ function queryTenorAndReply(
* Set image of embed if Tenor query returned any.
* If no results have been returned, set Description to tell user that nothing has been found.
*/
// ? embed.setDescription(`<@${message.author.id}> Es konnten keine Gifs gefunden werden für: '${searchQuery}'`)
embed =
Results.length === 0
? embed.setDescription(`<@${message.author.id}> Es konnten keine Gifs gefunden werden für: '${searchQuery}'`)
? embed.setDescription(
client.translate({
key: 'commands.fun.tenor.ErrorNoGifsFound',
options: { userID: message.author.id, searchQuery: searchQuery, lng: message.author.language },
}),
)
: embed.setImage(Results[0].media.find(element => Object.prototype.hasOwnProperty.call(element, 'gif')).gif.url)

/**
Expand Down
17 changes: 17 additions & 0 deletions commands/tests/hello.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Message } from 'discord.js'

import { DiscordClient } from '../../types/customTypes'

exports.name = 'hello'

exports.description = 'Hallo welt!'

exports.usage = 'hello'

exports.aliases = ['hallo']

exports.run = (client: DiscordClient, message: Message, language: string) => {
const returnString = client.translate({ key: 'commands.tests.hello.welcome', lng: language })

return client.reply(message, { content: returnString })
}
8 changes: 4 additions & 4 deletions commands/utility/antwortaufalles.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Message, MessageEmbed } from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { MessageEmbed } from 'discord.js'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'

exports.name = 'antwortaufalles'

exports.description = 'Was ist die Antwort auf alles?'

exports.usage = 'antwortaufalles'

exports.run = (client: DiscordClient, message: Message) =>
exports.run = (client: DiscordClient, message: DiscordMessage) =>
/**
* Reply with the anwer to everything.
*/
client.reply(message, {
embeds: [
new MessageEmbed().setDescription(
'Die Antwort auf die Frage nach dem Leben, dem Universum und dem ganzen Rest ist :four::two:',
client.translate({ key: 'commands.utility.antwortaufalles', lng: message.author.language }),
),
],
})
24 changes: 17 additions & 7 deletions commands/utility/channelinfo.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import {
Channel,
GuildMember,
Message,
MessageEmbed,
MessageMentions,
NewsChannel,
PartialDMChannel,
TextChannel,
ThreadChannel,
} from 'discord.js'
import { DiscordClient } from '../../types/customTypes'
import { DiscordClient, DiscordMessage } from '../../types/customTypes'
exports.name = 'channelinfo'

exports.description = 'Displays some information about the channel and its users.'

exports.usage = 'channelinfo {#channel}'

exports.run = async (client: DiscordClient, message: Message, args: string[]) => {
exports.run = async (client: DiscordClient, message: DiscordMessage, args: string[]) => {
/**
* Channel to display information about.
*/
Expand Down Expand Up @@ -44,7 +43,16 @@ exports.run = async (client: DiscordClient, message: Message, args: string[]) =>
embeds: [
new MessageEmbed()
.setAuthor({ name: client.user.tag, iconURL: client.user.avatarURL() })
.setTitle(`Anzahl an Kanalmitgliedern: ${CHANNEL.members.size.toString()}\nin ${CHANNEL.name}`)
.setTitle(
client.translate({
key: 'commands.utility.channelinfo.Answer',
options: {
memberNumber: CHANNEL.members.size.toString(),
channelName: CHANNEL.name,
lng: message.author.language,
},
}),
)
.setColor('BLURPLE')
.setDescription(cutUserList),
],
Expand All @@ -55,7 +63,9 @@ exports.run = async (client: DiscordClient, message: Message, args: string[]) =>
*/
return client.reply(message, {
embeds: [
new MessageEmbed().setDescription('Nicht in diesem Kanal verfügbar. Probiere es doch in einem anderen Kanal.'),
new MessageEmbed().setDescription(
client.translate({ key: 'commands.utility.channelinfo.ErrorNotAvailable', lng: message.author.language }),
),
],
})
}
Expand All @@ -65,13 +75,13 @@ exports.run = async (client: DiscordClient, message: Message, args: string[]) =>
* Helper function to get channel.
* @param { string[] } args message arguments
* @param { DiscordClient } client Bot client
* @param { Message } message issued command message
* @param { DiscordMessage } message issued command message
* @returns { Channel | PartialDMChannel | TextChannel | NewsChannel | ThreadChannel }
*/
async function getChannel(
args: string[],
client: DiscordClient,
message: Message<boolean>,
message: DiscordMessage,
): Promise<Channel | PartialDMChannel | TextChannel | NewsChannel | ThreadChannel> {
const _channel = args.find(value => MessageMentions.CHANNELS_PATTERN.test(value))?.slice(2, -1)
const CHANNEL = _channel ? await client.channels.fetch(_channel) : message.channel
Expand Down
Loading

0 comments on commit ece3241

Please sign in to comment.