質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

解決済

1回答

1735閲覧

Discord.js v14のスラッシュコマンドで任意のチャンネルにメッセージを送信する。

kanashiikana

総合スコア11

Discord

Discordは、ゲーマー向けのボイスチャットアプリです。チャット・通話がブラウザ上で利用可能で、個人専用サーバーも開設できます。通話中でも音楽を流したり、PC画面を共有できるなど多機能な点が特徴です。

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

1クリップ

投稿2022/09/21 14:45

編集2022/09/24 08:53

前提

Discord.js v14.4.0
node v18.4.0

実現したいこと

Discord.js v14のスラッシュコマンドでinteraction.replyでない方法で任意のチャンネルにメッセージを送信したい

発生している問題・エラーメッセージ

メッセージが送れない。

該当のソースコード

js

1const {Client,Collection,GatewayIntentBits,Partials} = require('discord.js'); 2const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages], partials: [Partials.Channel]}); 3const { ApplicationCommandType, ApplicationCommandOptionType } = require('discord.js'); 4module.exports = { 5 data: { 6 name: 'test', 7 description: 'テスト', 8 type: ApplicationCommandType.ChatInput 9 }, 10 async execute(interaction) { 11 await interaction.reply({content: "hello", ephemeral: true}); 12 client.channels.get('送信するチャンネルのID').send('hogefuga'); 13 }, 14}; 15

補足

index.jsの中身はこんな感じです。

js

1const Discord = require("discord.js"); 2const {Client,Collection,GatewayIntentBits,Partials} = require('discord.js'); 3const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages], partials: [Partials.Channel]}); 4const {ApplicationCommandType, ApplicationCommandOptionType} = require('discord.js'); 5const {ButtonStyle} = require('discord.js'); 6const {exec} = require('child_process'); 7const fs = require('fs'); 8const dotenv = require('dotenv'); 9dotenv.config(); 10 11 12const commands = {}; 13const commandFiles = fs.readdirSync(`./commands`).filter(file => file.endsWith(".js")); 14 for (const file of commandFiles) { 15 const command = require(`./commands/${file}`); 16 console.log(`${command.data.name} がロードされました。`); 17 try { 18 commands[command.data.name] = command; 19 } catch (error) { 20 console.log(`\u001b[31m${command.data.name} はエラーによりロードされませんでした。\nエラー内容\n ${error}\u001b[0m`); 21 } 22} 23 24client.once("ready", async () => { 25 const data = [] 26 for (const commandName in commands) { 27 data.push(commands[commandName].data) 28 } 29 await client.application.commands.set(data, 'サーバーのID'); 30}); 31 32client.on("interactionCreate", async (interaction) => { 33 if (!interaction.isChatInputCommand()) return; 34 const command = commands[interaction.commandName]; 35 if (!command) return; 36 37 if (command.guildOnly && !interaction.inGuild()) { 38 await interaction.reply({ 39 content: 'このコマンドはDMでは使えません。', 40 ephemeral: true, 41 }) 42 return; 43 } 44 try { 45 await command.execute(interaction); 46 } catch (error) { 47 console.error(error); 48 await interaction.reply({ 49 content: 'コマンド実行時にエラーが発生しました。', 50 ephemeral: true, 51 }) 52 } 53 54}); 55 56 57const eventFiles = fs.readdirSync('./events').filter(file => file.endsWith('.js')); 58for (const file of eventFiles) { 59 const event = require(`./events/${file}`); 60 if (event.once) { 61 client.once(event.name, (...args) => event.execute(...args, client)); 62 } 63 else { 64 client.on(event.name, (...args) => event.execute(...args, client)); 65 } 66} 67 68client.login().then(() => { 69 client.user.setPresence({activities: [{name: 'テスト'}], status: 'online'}); 70}); 71

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

js

1const {Client,Collection,GatewayIntentBits,Partials,ApplicationCommandType, ApplicationCommandOptionType} = require('discord.js'); 2const client = new Client({intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages], partials: [Partials.Channel]}); 3module.exports = { 4 data: { 5 name: 'test', 6 description: 'テスト', 7 type: ApplicationCommandType.ChatInput 8 }, 9 async execute(interaction) { 10 await interaction.reply({content: "hello", ephemeral: true}); 11 var channel = interaction.guild.channels.cache.find((channel) => channel.name === "チャンネル名").id; 12 await interaction.guild.channels.cache.get(channel).send("hello"); 13 }, 14}; 15

でできました。

投稿2022/09/22 08:00

編集2022/09/23 16:56
kanashiikana

総合スコア11

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問