Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 9409

discord.jsでDiscord Botを作ってみた

$
0
0

概要

discord.jsでbotを作ったので、それを書き留めときます。
インストールは、Ubuntu mateを想定したものを紹介致します。
他のOSを使っている方に関しては、調べてください

Node.js&npmのインストール方法

コマンドラインを開き下のコマンドを実行します。

sudo apt install -y nodejs npm

discord.jsのインストール方法

こちらもコマンドラインを開き下のコマンドを実行します。

sudo npm i discord.js

実際に書いていく

まずは、discord.jsを読み込むコードを書きます。

index.js
constdiscord=require("discord.js");constclient=newdiscord.Client;

簡単な返事機能のコードを書きます。

index.js
//続きclient.on("message",message=>{if(message.author.bot||!message.guild)returnif(message.content==="こんにちは"){message.reply('さん、こんにちはー');}});client.login("Botのトークン")

Botのトークンのところには、Discord Developer Portal
にある、自分のBotの右にあるBotへ行きtokenの下にあるcopyを押して、index.jsのBotのトークンのところへ貼り付けます。

実際にBotを起動してみる

Botを起動させるためにはコマンドラインで下のコマンドを実行します。

node index.js

ちゃんとBotがONLINEになっていれば成功です。
色々な種類のコードを書いていく予定ですので、よろしくお願いします。


Viewing all articles
Browse latest Browse all 9409

Trending Articles