From 7d912fe3f842386f1d31cf9f5a5947ebcb04e5f3 Mon Sep 17 00:00:00 2001 From: gx1285 Date: Thu, 1 Aug 2024 11:06:26 +0900 Subject: [PATCH] =?UTF-8?q?feat(AutoPublish):=20=E3=83=8B=E3=83=A5?= =?UTF-8?q?=E3=83=BC=E3=82=B9=E3=83=81=E3=83=A3=E3=83=B3=E3=83=8D=E3=83=AB?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E5=85=AC=E9=96=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/event/messageCreate/autopublish.ts | 26 ++++++++++ packages/bot/src/func/slash/auto-publish.ts | 17 ------ packages/bot/src/func/slash/setting.ts | 52 ++++++++++++++++++- packages/bot/src/func/slash/weather.ts | 2 +- .../bot/src/lib/db/entities/AutoPublish.ts | 7 +++ packages/bot/src/lib/db/entities/index.ts | 3 +- 6 files changed, 87 insertions(+), 20 deletions(-) create mode 100644 packages/bot/src/event/messageCreate/autopublish.ts delete mode 100644 packages/bot/src/func/slash/auto-publish.ts create mode 100644 packages/bot/src/lib/db/entities/AutoPublish.ts diff --git a/packages/bot/src/event/messageCreate/autopublish.ts b/packages/bot/src/event/messageCreate/autopublish.ts new file mode 100644 index 0000000..5481dc0 --- /dev/null +++ b/packages/bot/src/event/messageCreate/autopublish.ts @@ -0,0 +1,26 @@ +import { ChannelType, Message } from 'discord.js'; +import { MessageEventClass } from '../../lib/index.js'; +import { AutoPublish } from '../../lib/db/entities/AutoPublish.js'; +import { dataSource } from '../../lib/db/dataSource.js'; + +export default class implements MessageEventClass { + private async registered(channelId: string): Promise<{ bool: boolean; data: AutoPublish | undefined }> { + return dataSource.transaction(async (em) => { + const repo = em.getRepository(AutoPublish); + const data = await repo.findOneBy({ channelId }); + + if (!data) return { bool: false, data: undefined }; + return { bool: true, data }; + }); + } + async run(message: Message) { + if (!(await this.registered(message.channelId)).bool) return; + if (message.author.system) return; + if (message.channel.type !== ChannelType.GuildAnnouncement) return; + if (message.crosspostable) { + message.crosspost().then(() => message.react('✅')); + } else { + message.react('❌'); + } + } +} diff --git a/packages/bot/src/func/slash/auto-publish.ts b/packages/bot/src/func/slash/auto-publish.ts deleted file mode 100644 index 96d80c2..0000000 --- a/packages/bot/src/func/slash/auto-publish.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SlashCommandClass } from '../../lib/bot/index.js'; -import { ChannelType, ChatInputCommandInteraction, SlashCommandBuilder } from 'discord.js'; - -export default class AutoPublish implements SlashCommandClass { - command = new SlashCommandBuilder() - .setName('auto-publish') - .setDescription('ニュースチャンネルでの、自動公開機能を有効・無効化します') - .addChannelOption((input) => - input - .addChannelTypes(ChannelType.GuildAnnouncement) - .setName('channel') - .setDescription('チャンネル') - .setRequired(false), - ) - .setDMPermission(false); - async run(interaction: ChatInputCommandInteraction) {} -} diff --git a/packages/bot/src/func/slash/setting.ts b/packages/bot/src/func/slash/setting.ts index c108b33..35a9381 100644 --- a/packages/bot/src/func/slash/setting.ts +++ b/packages/bot/src/func/slash/setting.ts @@ -11,12 +11,14 @@ import { ButtonBuilder, ButtonStyle, ButtonInteraction, + ChannelType, } from 'discord.js'; import { Ai } from '../../lib/db/entities/Ai.js'; import { dataSource } from '../../lib/db/dataSource.js'; import { EarthQuakeAlert } from '../../lib/db/entities/EarthQuakeAlert.js'; +import { AutoPublish } from '../../lib/db/entities/AutoPublish.js'; -export default class AutoPublish implements SlashCommandClass { +export default class Setting implements SlashCommandClass { command = new SlashCommandBuilder() .setName('setting') .setDescription('Aquedの設定をします。') @@ -30,6 +32,7 @@ export default class AutoPublish implements SlashCommandClass { ) .setDMPermission(false); channelId: string; + channelType: ChannelType; makeMessage(first: boolean, bool?: boolean, content?: string): MessagePayload | BaseMessageOptions { const func = new StringSelectMenuBuilder() .setPlaceholder('機能を選択') @@ -45,6 +48,15 @@ export default class AutoPublish implements SlashCommandClass { .setValue('earthquake'), ) .setMaxValues(1); + + if (this.channelType === ChannelType.GuildAnnouncement) + func.addOptions( + new StringSelectMenuOptionBuilder() + .setLabel('publish') + .setDescription('ニュースチャンネルでの、自動公開機能を有効・無効にします') + .setValue('publish'), + ); + const boolButton = new ButtonBuilder(); if (!bool) boolButton.setLabel('有効にする').setCustomId('setting_button_false').setStyle(ButtonStyle.Success); else boolButton.setLabel('無効にする').setCustomId('setting_button_true').setStyle(ButtonStyle.Danger); @@ -58,6 +70,7 @@ export default class AutoPublish implements SlashCommandClass { if (interaction.options.getSubcommand() !== 'channel') return; const channel = interaction.options.getChannel('チャンネル'); this.channelId = channel.id; + this.channelType = channel.type; await interaction.reply(this.makeMessage(true, false, '設定する項目をお選びください')); } async button(interaction: ButtonInteraction) { @@ -70,6 +83,9 @@ export default class AutoPublish implements SlashCommandClass { } else if (interaction.message.content.includes('地震速報')) { const bool = await this.eqRegister(); await this.response('地震速報', bool, interaction); + } else if (interaction.message.content.includes('自動公開')) { + const bool = await this.apRegister(); + await this.response('自動公開', bool, interaction); } } } @@ -95,7 +111,16 @@ export default class AutoPublish implements SlashCommandClass { break; } + case 'publish': { + if (!this.channelId) + return await interaction.update({ content: '設定パネルの使用期限が切れています', components: [] }); + const { bool } = await this.apRegistered(); + await interaction.update( + this.makeMessage(false, bool, bool ? '**自動公開: __登録__**' : '**自動公開: __未登録__**'), + ); + break; + } default: break; } @@ -161,4 +186,29 @@ export default class AutoPublish implements SlashCommandClass { } }); } + private async apRegistered(): Promise<{ bool: boolean; data: AutoPublish | undefined }> { + return dataSource.transaction(async (em) => { + const channelId = this.channelId; + const repo = em.getRepository(AutoPublish); + const data = await repo.findOneBy({ channelId }); + + if (!data) return { bool: false, data: undefined }; + return { bool: true, data }; + }); + } + private async apRegister() { + return dataSource.transaction(async (em) => { + const registered = await this.eqRegistered(); + const repo = em.getRepository(AutoPublish); + if (registered.bool) { + repo.delete(registered.data); + return false; + } else { + const data = new AutoPublish(); + data.channelId = this.channelId; + repo.save(data); + return true; + } + }); + } } diff --git a/packages/bot/src/func/slash/weather.ts b/packages/bot/src/func/slash/weather.ts index fec87f1..84a4990 100644 --- a/packages/bot/src/func/slash/weather.ts +++ b/packages/bot/src/func/slash/weather.ts @@ -96,7 +96,7 @@ export default class implements SlashCommandClass { }); } catch (error) { await interaction.editReply({ content: `データ取得にエラーが発生しました` }); - Logger.error(error); + interaction.client.logger.error(error); } } async autoComplete(interaction: AutocompleteInteraction) { diff --git a/packages/bot/src/lib/db/entities/AutoPublish.ts b/packages/bot/src/lib/db/entities/AutoPublish.ts new file mode 100644 index 0000000..cd73e25 --- /dev/null +++ b/packages/bot/src/lib/db/entities/AutoPublish.ts @@ -0,0 +1,7 @@ +import { Entity, PrimaryColumn } from 'typeorm'; + +@Entity({ name: 'AUTO_PUBLISH' }) +export class AutoPublish { + @PrimaryColumn({ name: 'CHANNEL_ID', type: 'bigint', comment: 'チャンネルID' }) + channelId: string; +} diff --git a/packages/bot/src/lib/db/entities/index.ts b/packages/bot/src/lib/db/entities/index.ts index 0b266c2..f4e3164 100644 --- a/packages/bot/src/lib/db/entities/index.ts +++ b/packages/bot/src/lib/db/entities/index.ts @@ -3,6 +3,7 @@ import { AFKMentions } from './AFKMention.js'; import { Ai } from './Ai.js'; import { AiThread } from './AiThread.js'; import { AiThreadHistory } from './AiThreadHistory.js'; +import { AutoPublish } from './AutoPublish.js'; import { EarthQuakeAlert } from './EarthQuakeAlert.js'; import { GBAN } from './GBAN.js'; -export const entities = [AFK, AFKMentions, EarthQuakeAlert, Ai, AiThread, AiThreadHistory, GBAN]; +export const entities = [AFK, AFKMentions, EarthQuakeAlert, Ai, AiThread, AiThreadHistory, GBAN, AutoPublish];