LoginSignup
3
2

More than 5 years have passed since last update.

discordrbでBotにEmbedを送信させる

Posted at

やりたいこと

Discord向けのBotを作るためのgemであるdiscordrb でEmbedを送りたい.

Embedとは

通常のチャットメッセージではなく,thumbnailやauthorを指定することで決められたフォーマットに沿って整形されるメッセージのことです.

embed.PNG

DiscordでURLを含んだメッセージを送信した時に,URLのWebページに関する内容がインライン表示されている部分がEmbedです.もし,SlackのIncoming Webhookを利用した経験があるなら, Attachment と考えれば分かりやすいかもしれません.

discordrbでBotにEmbedを送信させるサンプル

require 'discordrb'

# Discord botのTOKEN,CLIENT_IDを指定する
bot = Discordrb::Bot.new token: TOKEN, client_id: CLIENT_ID

bot.message(with_text: 'bot embed') do |event|
  event.channel.send_embed do |embed|
    embed.title = 'fugafuga'
    embed.url = 'hoge.com'
    embed.author = Discordrb::Webhooks::EmbedAuthor.new(name: 'hoge-san')
  end
end

bot.run

チャンネル内で「bot embed」と発言があった際に,Embedを送信するサンプルです.event.channel.send_embed do |embed|で発言のあったチャンネルに対してEmbedを送信することが出来ます.

titleやurlなどは単純に文字列を渡すだけで指定できますが,AuthorやImageはDiscordrb::Webhooks::EmbedAuthorを用いて指定する必要があります.

Class: Discordrb::Webhooks::Embed

その他:Embedをプレビューする

Discord Embed Generator
Webページ上でEmbedとして表示したい情報を入力することでプレビューできて便利です.

3
2
0

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