LoginSignup
15
12

More than 3 years have passed since last update.

rubyで簡単discordbot

Last updated at Posted at 2017-12-27

ストックじゃなくていいねください。

discordのbotを作成

  1. ここでNew Appをクリック
  2. APP NAMEとAPP ICONを設定し、Create Appをクリック
  3. Client Secret: click to reveal のclick to revealをクリックし、ここでClient IDとClient Secretを取得。(メモしておいてください。)
  4. BotのCreate a Bot Userをクリック。BotのToken:click to revealのclick to revealをクリックし、Tokenを取得。(これもメモしておいてください。)
  5. 最初はpublic botに設定しておいてください。 (botをサーバーに追加したあとに、Require OAuth2 Code Grantにチェックを入れて、public botのチェックを外してください。 )
  6. discordapp.com/oauth2/authorize?client_id=ClientID&scope=bot&permissions=0 このurlのClientIDを先程取得したClientIDに変更してurl先で接続させたいサーバーを選択。 権限がないとサーバーに追加できないので、権限がない人は権限をもらってください。

ここまでで、とりあえずサーバーにbotを追加できました。

サーバーにbotを起動

ping.rb
require 'discordrb'
bot = Discordrb::Bot.new token: 'ここにトークン'##discordのbotトークン
puts "This bot's invite URL is #ここに招待URL."##discordの招待URL
puts 'Click on it to invite it to your server.'
bot.message(content: 'Ping!') do |event|
  event.respond 'Pong!'
end

bot.run

gem

$ gem install discordrb

Bundlerでインストール

Gemfileを下記のようにgem "discordrb"を追記

source "https://rubygems.org"
# gem "rails"
gem "discordrb"
$ bundle install --path vendor/bundler
bundle exec ruby ping.rb

discordで Ping!とメッセージを送信すると、Pong!と出力されればbotとサーバーの接続は完了です。

botプログラムソース

今回はR6sのキャラをランダムに出力させます。

require 'discordrb'
bot = Discordrb::Bot.new token: 'discordのトークンをここに'
puts "This bot's invite URL is #https://discord.gg/xxxxxx."#discordのurl
puts 'Click on it to invite it to your server.'

attackers = ['SLEDGE(スレッジ)',
'THATCHER(サッチャー)',
'ASH(アッシュ)',
'THERMITE(テルミット)',
'TWITCH(トゥイッチ)',
'MONTAGNE(モンターニュ)',
'GLAZ(グラズ)',
'FUZE(フューズ)',
'BLITZ(ブリッツ)',
'IQ(アイキュー)',
'BUCK(バック)',
'BLACKBEARD(ブラックビアード)',
'CAPITAO(キャピタオ)',
'HIBANA(火花)',
'JACKAL(ジャッカル)',
'YING(イン)',
'ZOFIA(ゾフィア)',
'DOKKAEBI(ドッカェビィ)'
]
defenders = ['SMOKE(スモーク)',
'MUTE(ミュート)',
'PULSE(パルス)',
'DOC(ドク)',
'ROOK(ルーク)',
'KAPKAN(カプカン)',
'TACHANKA(タチャンカ)',
'JAGER(イェーガー)',
'BANDIT(バンディット)',
'FROST(フロスト)',
'VALKYRIE(ヴァルキリー)',
'CAVEIRA(カヴェイラ)',
'ECHO(エコー)',
'MIRA(ミラ)',
'LESION(リージョン)',
'ELA(エラ)',
'VIGIL(ヴィジル)'
]
#シージのキャラ
operators = attackers + defenders


#"plz attack"とチャットに入力があった場合反応
bot.message(content: 'plz atk') do |event|
  event.respond "#{event.user.name}:#{attackers.sample}"
end

#"plz defense"とチャットに入力があった場合反応
bot.message(content: 'plz def') do |event|
  event.respond "#{event.user.name}:#{defenders.sample}"
end

#"plz character"とチャットに入力があった場合反応
bot.message(content: 'plz character') do |event|
  event.respond "#{event.user.name}:#{operators.sample}"
end


bot.run

ここには
mapの見取り図を出力させたりできるようにしたものが置いてあります。

次はGooglespreadSheets

次回はGooglespreadSheetsの中身をdiscordにpostさせます。

15
12
2

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
15
12