4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Discord.jsでグローバルチャットを作る

Last updated at Posted at 2020-04-30

Discord.pyのグローバルチャットについては@coolwind0202さんが[Discord.pyでグローバルチャット](https://qiita.com/coolwind0202/items/061a08b0608d94fcb552)という記事を書いていたので、そちらをご覧ください。
Discord.jsのインストールなどは省略するので、自分で調べてください。

#追記
220/04/30 画像が表示されないバグを直しました。

グローバルチャットとは

グローバルチャットとは、あるサーバーAでメッセージを送信すると、他のサーバーBやCでもメッセージを受信できるというものです。また、Bでメッセージを送信するとAやCでもメッセージを受信できます。

説明が下手なのでこちらを見てもらった方が早いと思います。

#コード
グローバルチャットのコードです。コードについての解説は下を見てください。

discordbot.js
client.on('message', message => {
	if(message.author.bot){
		return;
	}
	if (message.channel.name == ("グローバルチャット")){
		message.delete();
		const ch_name = "グローバルチャット";
		client.channels.forEach(channel => {
			if (channel.name === ch_name) {
				channel.send({embed: {
					title: message.content,
					color: 0x800080,
					timestamp: new Date(),
					footer: {
						text: "TestBotのグローバルチャット"
					},
					thumbnail: {
						url: "https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.png"
					},
					fields: [
						{
							name: "サーバー",
							value: `${message.guild.name} (${(message.guild.id)})`,
							inline: true
						},
						{
							name: "チャンネル",
							value: `${message.channel.name} (${message.channel.id})`,
							inline: true
						},
						{
							name: "ユーザー",
							value: `${message.author.username} (${message.author.id})`,
							inline: true
						}
					]
				}});
			}
		})
	}
});

#コード解説
ここではコードの解説をしていきます。

Discord.js
client.on('message', message => { // メッセージが送信されたとき
	if(message.author.bot){ // もし相手がBOTなら無視する
		return;
	}
	if (message.channel.name == ("グローバルチャット")){ // もし送信された場所が「グローバルチャット」なら
		message.delete(); // メッセージを削除する(二重になるのを防ぐため)
		const ch_name = "グローバルチャット"; // 「グローバルチャット」という名前のチャンネルに一斉配信
		client.channels.forEach(channel => {
			if (channel.name === ch_name) {
				channel.send({embed: {
					title: message.content, // メッセージ内容
					color: 0x800080, // 色
					timestamp: new Date(),
					footer: {
						text: "TestBotのグローバルチャット" //フッターの内容
					},
					thumbnail: {
						url: "https://cdn.discordapp.com/avatars/${message.author.id}/${message.author.avatar}.png" // ユーザーのアイコン
					},
					fields: [
						{
							name: "サーバー",
							value: `${message.guild.name} (${(message.guild.id)})`, // 送信されたサーバーの名前とID
							inline: true
						},
						{
							name: "チャンネル",
							value: `${message.channel.name} (${message.channel.id})`, // 送信されたチャンネルの名前とID
							inline: true
						},
						{
							name: "ユーザー",
							value: `${message.author.username} (${message.author.id})`, // 送信したユーザーの名前とID
							inline: true
						}
					]
				}});
			}
		})
	}
});

embedについてはこちらの記事をご覧ください。

#完成!
image.png
メッセージを送信すると、このような感じになります。

4
3
1

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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?