0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Discord.js】ニュースチャンネルをフォローするコマンドの作り方解説

Last updated at Posted at 2025-09-16

Discord.js v14 を使って、特定のニュースチャンネルをフォローしたチャンネルを作成するコマンドの実装方法の解説です。

コマンドにさせたい動作

・特定のアナウンスチャンネルを取得
・新しいテキストチャンネルを作成
・作成したチャンネルにアナウンスチャンネルのフォロー設定を行う

コード

follow-announce.js
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

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?