Skip to content

Commit

Permalink
Merge pull request #27 from CHProducts/dev
Browse files Browse the repository at this point in the history
英語への翻訳コマンド追加
  • Loading branch information
MotiCAT authored Jan 26, 2024
2 parents 24333a5 + c826461 commit 3e9bf3c
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ CLIENT_ID=
OWNER_ID=
MONGO_URL=
GOOGLE_KEY=
GOOGLE_CX=
GOOGLE_CX=
DEEPL_API_KEY=
5 changes: 4 additions & 1 deletion src/Events/onInteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ export async function onInteractionCreate(interaction: BaseInteraction): Promise
if (interaction.isMessageContextMenuCommand()) {
switch (interaction.commandName) {
case '日本語に翻訳':
interactions.translate(interaction);
interactions.ja_translate(interaction);
break;
case '英語に翻訳':
interactions.en_translate(interaction);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/deploy-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,8 @@ const commands = [
.setName('user')
.setDescription('ユーザーの情報を表示します')
.addUserOption((option) => option.setName('user').setDescription('ユーザー')),
new ContextMenuCommandBuilder().setName('日本語に翻訳').setType(ApplicationCommandType.Message)
new ContextMenuCommandBuilder().setName('日本語に翻訳').setType(ApplicationCommandType.Message),
new ContextMenuCommandBuilder().setName('英語に翻訳').setType(ApplicationCommandType.Message)
];

const rest = new REST().setToken(process.env.DISCORD_TOKEN!);
Expand Down
25 changes: 25 additions & 0 deletions src/interactions/context_en_translate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { embeds } from '../embeds';
import translate from 'deepl';
import { EmbedBuilder, MessageContextMenuCommandInteraction } from 'discord.js';

export async function en_translateCommand(interaction: MessageContextMenuCommandInteraction) {
if (!interaction.targetMessage.content) return interaction.reply(embeds.translateError);
translate({
text: interaction.targetMessage.content,
target_lang: 'EN',
auth_key: process.env.DEEPL_API_KEY!,
free_api: true
})
.then((res) => {
const embed = new EmbedBuilder()
.setTitle('Translation Result')
.setDescription(res.data.translations[0].text)
.setColor('#0099ff')
.setFooter({ text: 'Powered by DeepL' });
interaction.reply({ embeds: [embed] });
})
.catch((err) => {
console.error(err);
interaction.reply(embeds.translateError);
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { embeds } from '../embeds';
import translate from 'deepl';
import { EmbedBuilder, MessageContextMenuCommandInteraction } from 'discord.js';

export async function translateCommand(interaction: MessageContextMenuCommandInteraction) {
export async function ja_translateCommand(interaction: MessageContextMenuCommandInteraction) {
if (!interaction.targetMessage.content) return interaction.reply(embeds.translateError);
translate({
text: interaction.targetMessage.content,
Expand Down
6 changes: 4 additions & 2 deletions src/interactions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { noCommand } from './button_no';
import { yesCommand } from './button_yes';
import { clearCommand } from './clear';
import { closeticketCommand } from './closeticket';
import { translateCommand } from './context_translate';
import { en_translateCommand } from './context_en_translate';
import { ja_translateCommand } from './context_ja_translate';
import { createticketCommand } from './createticket';
import { helpCommand } from './help';
import { inlistCommand } from './inlist';
Expand Down Expand Up @@ -59,5 +60,6 @@ export const interactions = {
yes: yesCommand,
no: noCommand,
responseComplete: responseComplete,
translate: translateCommand
ja_translate: ja_translateCommand,
en_translate: en_translateCommand
};

0 comments on commit 3e9bf3c

Please sign in to comment.