LoginSignup
4
1

More than 3 years have passed since last update.

Discord botを試してみる

Last updated at Posted at 2020-12-24

本記事はFusic Advent Calendar 2020の24日目の記事です。

自分で管理しているDiscordがあるんですが、そこにbotを突っ込んでみたくて突っ込みました。

どんなものかって言うと、議論しながら実行結果が見えればいいなぁという試みでコード結果を返すボットです。

Discord botを作る

Discordでbotを作るために、以下を参考にしました。
https://qiita.com/1ntegrale9/items/aa4b373e8895273875a8

具体的には、
1. https://discordapp.com/developers/applications へアクセスして、アカウント作成
2. 左メニューのBotからAdd BotからサーバーへBotの追加
3. New Applicationからアプリの作成
4. 自分の書いたコードを適当なサーバーで起動

という流れです。
僕は今回herokuへ上げました。簡単なので、おすすめです。

実装

ボットを起動する部分だけ記載しておきます。
(具体的には、送られたコードブロックを解析する形で記述しました。)

module CodeBlockExecuter

using Discord


# Create a handler for the MessageCreate event.
function handler(c::Client, e::MessageCreate)
  body = e.message.content

  if !ismissing(e.message.author.bot)
    return
  end

  # メッセージのパース処理…
 parseresult = ....

 # 返答
  reply(c, e.message, parseresult)
end

function main()
  # Create a client.
  c = Client(ENV["ACCESS_TOKEN"])
  # Add the handler.
  add_handler!(c, MessageCreate, handler)
  # Log in to the Discord gateway.
  open(c)
  # Wait for the client to disconnect.
  wait(c)
end

end

なお、Discord.jlのexampleを参考にしています。
他にも、リアクションを飛ばせる
create(c, Reaction, e.message, '👍')のも使えたりします。

結果

31EE0046-BEAE-4972-B4B4-7386EC2D3F56.jpeg

ちゃんと動いてくれることが確認できました。

他に

Dynoというカスタムコマンド等を追加できるものもあるようです。
けど、コードでカスタムしたりといったことはできないっぽい?かも?

slackみたいにスラッシュコマンドで実行できる形にできると便利ですね。

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