Skip to content

Commit

Permalink
feat(AutoPublish): ニュースチャンネル自動公開
Browse files Browse the repository at this point in the history
  • Loading branch information
gx1285 committed Aug 1, 2024
1 parent b2f6737 commit 7d912fe
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 20 deletions.
26 changes: 26 additions & 0 deletions packages/bot/src/event/messageCreate/autopublish.ts
Original file line number Diff line number Diff line change
@@ -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('❌');
}
}
}
17 changes: 0 additions & 17 deletions packages/bot/src/func/slash/auto-publish.ts

This file was deleted.

52 changes: 51 additions & 1 deletion packages/bot/src/func/slash/setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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の設定をします。')
Expand All @@ -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('機能を選択')
Expand All @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
}
}
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
});
}
}
2 changes: 1 addition & 1 deletion packages/bot/src/func/slash/weather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 7 additions & 0 deletions packages/bot/src/lib/db/entities/AutoPublish.ts
Original file line number Diff line number Diff line change
@@ -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;
}
3 changes: 2 additions & 1 deletion packages/bot/src/lib/db/entities/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];

0 comments on commit 7d912fe

Please sign in to comment.