前提
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

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。