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

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

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

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

JavaScript

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

Q&A

解決済

2回答

931閲覧

Discord bot またまたエラー

ibuki_rei

総合スコア95

Discord

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

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

JavaScript

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

0グッド

1クリップ

投稿2022/08/23 12:09

エラーを吐いたコード

node.js

1const http = require("http"); 2const querystring = require("querystring"); 3const discord = require("discord.js"); 4const client = new discord.Client(); 5const prefix = process.env.prefix; 6try { 7 // GAS(Google Apps Script)からの受信(botの常時起動) 8 http.createServer(function(req, res){ 9 res.write("OK"); 10 res.end(); 11 }).listen(8080); 12 client.on("ready", message => { 13 console.log("Bot準備完了~"); 14 client.user.setActivity(process.env.activity, { type: process.env.acttype }); 15 }); 16client.on("message", async message => { 17 if (message.author.id == client.user.id || message.author.bot) return; 18 if (message.mentions.has(client.user)) { 19 message.reply("呼びましたか?"); 20 } 21 if (!message.content.startsWith(prefix)) return; //ボットのプレフィックスからメッセージが始まっているか確認 22 const args = message.content.slice(prefix.length).trim().split(/ +/g); 23 const command = args.shift().toLowerCase(); 24 if (command === "help") { //コマンド名 25 message.channel.send({ 26 embed: { 27 title: "ヘルプ", 28 description: "全てのコマンドの初めに`" + prefix + "`をつける必要があります。", 29 fields: [ 30 { 31 name: "ヘルプ", 32 value: "`help`" 33 } 34 ] 35 } 36 }); 37 } 38}); 39 const command = args.shift().toLowerCase(); 40 if (command === "mai") { //コマンド名 41 message.channel.send({ 42 embed: { 43 title: "mai", 44 description: "まいchはミルダム配信者,ですいーたーリスナー", 45 46 fields: [ 47 { 48 name: "mai", 49 value: "`b!`" 50 } 51 ] 52 } 53 }); 54 } 55 56 if (process.env.DISCORD_BOT_TOKEN == undefined) { 57 console.log("DISCORD_BOT_TOKENが設定されていません。"); 58 process.exit(0); 59 } 60 client.login(process.env.DISCORD_BOT_TOKEN); 61} catch (e) { 62 console.log(e); 63}

はいたエラー

error

1ReferenceError: args is not defined

前にも質問してエラーが起きたところを修正して起動させたら違うエラーが起こりました

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

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

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

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

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

guest

回答2

0

node.js

1const http = require("http"), 2 querystring = require("querystring"), 3 { Client, GatewayIntentBits, Partials } = require('discord.js'), 4 client = new Client({ 5 partials: [Partials.Channel], 6 intents: [ 7 GatewayIntentBits.Guilds, 8 GatewayIntentBits.GuildVoiceStates, 9 GatewayIntentBits.GuildMessages, 10 GatewayIntentBits.GuildMessageReactions, 11 GatewayIntentBits.DirectMessages, 12 GatewayIntentBits.MessageContent 13 ] 14 }), 15 config = { 16 "token": "-------", 17 "prefix": "b!" 18 }; 19var args, command; 20// GAS(Google Apps Script)からの受信(botの常時起動) 21http.createServer(function (req, res) { 22 res.write("OK"); 23 res.end(); 24}).listen(8080); 25client.on("ready", message => { 26 console.log("Bot準備完了~"); 27 client.user.setActivity(process.env.activity, { type: process.env.acttype }); 28}); 29client.on('messageCreate', message => { 30 if (message.author.bot) return; 31 32 if (message.mentions.has(client.user)) { 33 message.reply("呼びましたか?"); 34 35 } else if (message.content.startsWith(config.prefix)) { //ボットのプレフィックスからメッセージが始まっているか確認 36 args = message.content.slice(config.prefix.length).trim().split(/ +/g); 37 command = args.shift().toLowerCase(); 38 console.log(command); //コマンド内容コンソール出力&動作確認 39 if (command == "help") { //コマンド名 40 message.channel.send({ 41 embeds: [{ 42 title: "ヘルプ", 43 description: "全てのコマンドの初めに`" + config.prefix + "`をつける必要があります。", 44 url: '', 45 fields: [ 46 { name: "ヘルプ", value: "`help`" } 47 ], 48 }] 49 }); 50 } else if (command == "mai") { //コマンド名 51 message.channel.send({ 52 embeds: [{ 53 title: "mai", 54 description: "まいchはミルダム配信者、ですいーたーリスナー", 55 fields: [ 56 { name: "mai", value: "`" + config.prefix + "`" } 57 ], 58 }] 59 }); 60 }; 61 }; 62}), 63 64 client.login(config.token);

投稿2022/08/24 07:38

編集2022/08/25 00:35
ibuki_rei

総合スコア95

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

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

0

自己解決

node.js

1const http = require("http"), 2 querystring = require("querystring"), 3 { Client, GatewayIntentBits, Partials } = require('discord.js'), 4 client = new Client({ 5 partials: [Partials.Channel], 6 intents: [ 7 GatewayIntentBits.Guilds, 8 GatewayIntentBits.GuildVoiceStates, 9 GatewayIntentBits.GuildMessages, 10 GatewayIntentBits.GuildMessageReactions, 11 GatewayIntentBits.DirectMessages, 12 GatewayIntentBits.MessageContent 13 ] 14 }), 15 config = { 16 "token": "-------", 17 "prefix": "b!" 18 }; 19var args, command; 20// GAS(Google Apps Script)からの受信(botの常時起動) 21http.createServer(function (req, res) { 22 res.write("OK"); 23 res.end(); 24}).listen(8080); 25 26client.on("ready", () => { 27 console.log("Bot準備完了~"); 28 client.user.setActivity(process.env.activity, { type: process.env.acttype }); 29}); 30 31client.on('messageCreate', message => { 32 if (message.author.bot) return; //bot自身なら実行停止 33 if (message.mentions.has(client.user)) { 34 message.reply("呼びましたか?"); 35 36 } else if (message.content.startsWith(config.prefix)) { //ボットのプレフィックスからメッセージが始まっているか確認 37 args = message.content.slice(config.prefix.length).trim().split(/ +/g); 38 command = args.shift().toLowerCase(); 39 console.log(command); //コマンド内容コンソール出力&動作確認 40 if (command == "help") { //コマンド名 41 message.channel.send({ 42 embeds: [{ 43 title: "ヘルプ", 44 description: "全てのコマンドの初めに`" + config.prefix + "`をつける必要があります。", 45 url: '', 46 fields: [ 47 { name: "ヘルプ", value: "`help`" } 48 ], 49 }] 50 }); 51 } else if (command == "mai") { //コマンド名 52 message.channel.send({ 53 embeds: [{ 54 title: "mai", 55 description: "まいchはミルダム配信者、ですいーたーリスナー", 56 fields: [ 57 { name: "mai", value: "`" + config.prefix + "`" } 58 ], 59 }] 60 }); 61 }; 62 }; 63}); 64 65client.login(config.token);

うまくいきました!!

投稿2022/08/24 07:37

編集2022/08/25 00:35
ibuki_rei

総合スコア95

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

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

sumwave

2022/08/25 00:01 編集

自己解決、トークン乗ってるんでできれば早めに消すことをおすすめします。 ちなみにこれは、GASなどで動かす際にうまく動かず投げやりでトークンをjsに内蔵した結果です。 変更したのはargsなどの変数宣言をconstからvarに変更、一番上に置くことでエラー解消 messageCreateに置き換え、embedsの誤字修正 Diiscord.js v14へ対応させる、json内蔵(なんか相手がうまく行かないらしく、簡単に対処したらこうなりました) その他多少の変更が複数ありますが省略で
ibuki_rei

2022/08/25 00:34

今は僕ととあるかたで共同作業しておりGASは使っていませんconstからvarは変えてみますその他もろもろありがとうございます!!
ibuki_rei

2022/08/25 00:38

出来ればでいいんですけどローカルファイルにあるjsをGASで常時起動させる方法はありますか?    
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問