Discord.js v14 を使って、特定のニュースチャンネルをフォローしたチャンネルを作成するコマンドの実装方法の解説です。
コマンドにさせたい動作
・特定のアナウンスチャンネルを取得
・新しいテキストチャンネルを作成
・作成したチャンネルにアナウンスチャンネルのフォロー設定を行う
コード
const {
SlashCommandBuilder,
ChannelType,
PermissionFlagsBits,
MessageFlags,
channelMention,
InteractionContextType,
ApplicationIntegrationType,
} = require('discord.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('follow-announce')
.setDescription('お知らせチャンネルを作成します')
.setContexts([InteractionContextType.Guild])
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall])
.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels),
async execute(interaction) {
await interaction.deferReply({ flags: MessageFlags.Ephemeral });
const announcementChannelId = 'アナウンスチャンネルのID';
const newChannelName = 'お知らせチャンネル';
const announceChannel = await interaction.client.channels.fetch(announcementChannelId);
if (!announceChannel || announceChannel.type !== ChannelType.GuildAnnouncement) {
return interaction.editReply('指定したチャンネルはニュースチャンネルではありません。');
}
const botMember = interaction.guild.members.me;
const botPerms = botMember.permissions;
if (!botPerms.has(PermissionFlagsBits.ManageChannels)) {
return interaction.editReply('BOTにチャンネル作成権限がありません');
}
const existingChannel = interaction.guild.channels.cache.find(
(c) => c.name === newChannelName && c.type === ChannelType.GuildText
);
if (existingChannel) {
return interaction.editReply(
`既に同じ名前のチャンネル ${channelMention(existingChannel.id)} が存在します`
);
}
const followChannel = await interaction.guild.channels.create({
name: newChannelName,
type: ChannelType.GuildText,
});
await announceChannel.addFollower(followChannel.id);
await interaction.editReply(
`${channelMention(followChannel.id)} を作成しました`
);
},
};
コード説明
ここからコードの細かいとこの説明を行っていきます
使用制限
.setContexts([InteractionContextType.Guild])
.setIntegrationTypes([ApplicationIntegrationType.GuildInstall])
.setDefaultMemberPermissions(PermissionFlagsBits.ManageChannels),
サーバー限定、ギルドインストール限定、チャンネル編集権限があるユーザーのみ使用可
BOTの権限確認
const botMember = interaction.guild.members.me;
const botPerms = botMember.permissions;
if (!botPerms.has(PermissionFlagsBits.ManageChannels)) {
return interaction.editReply('ボットにサーバー全体でチャンネル作成権限がありません。');
}
botMember.pemissions
で権限を取得。
botperms.has()
で確認。
既存チャンネルの確認
const existingChannel = interaction.guild.channels.cache.find(
(c) => c.name === newChannelName && c.type === ChannelType.GuildText
);
if (existingChannel) {
return interaction.editReply(
`既に同じ名前のチャンネル ${channelMention(existingChannel.id)} が存在します`
);
}
コマンドを実行したサーバー内でnewChannelName
と同じテキストチャンネルがないかどうかの確認を行っています
テキストチャンネルを作成
const followChannel = await interaction.guild.channels.create({
name: newChannelName,
type: ChannelType.GuildText,
});
newsChannelNameで設定している名前でテキストチャンネルを作成します。必要なら
reason`も入れておいてください
アナウンスチャンネルをフォローし、テキストチャンネルに追加
await announceChannel.addFollower(followChannel.id);
addFollower()メソッドを使ってフォローを行います。
addFollower(textchannel, reason)
で追加を行うことができます
ただし、追加されるまでに少し時間がかかります。
完了メッセージを送信
await interaction.editReply(
`${channelMention(followChannel.id)} を作成しました`
);
channelMention()
を使って作成したチャンネルリンクを返すようにしています
まとめ
今回はニュースチャンネルをフォローしたチャンネルを作成するコマンドを作成する方法の解説でした。
調べてた時本当にこれに言及してる人が少なすぎて死んでたので書いておきます。
reddit民 書いておいてくれてありがとう
もしわからないことや、リクエストがあればコメントお願いします🙏
私の自鯖のDiscord危険情報共有鯖です
ユーザー数3400人突破しました!!
荒らしユーザーや対策に関する情報のまとめ鯖です
ギルドタグ「開示請求」もあるよ🔥
twitter🐦️
youtube