1
1

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 1 year has passed since last update.

discordiaでdiscordのBOTを作る際に躓いたところ

Last updated at Posted at 2023-06-18

discordiaとは

Discordia: Discordiaは、Lua(具体的にはLuvit)を使用してDiscordのBOTを作成するためのライブラリです。DiscordのAPIに対するラッパーとして機能し、ユーザーがDiscordのBOTを簡単に作成できるようにします。Discordのメッセージの送受信、チャンネルの管理、ユーザーとのインタラクションなど、DiscordのBOTが必要とする機能を提供します。
discordiaのgithub

ついでに...

Luvit: Luvitは、LuaとNode.jsの概念を組み合わせたプラットフォームです。Luaの軽量さと効率性、Node.jsの非同期I/Oとイベント駆動型のプログラミングの特性を組み合わせています。これにより、ウェブサーバー、リアルタイム通信アプリケーションなど、非同期I/Oを必要とするアプリケーションをLuaで効率的に開発することができます。

(以上の文章の大部分はChat-GPT4により生成されたものを採用しています。)

discordiaの導入

1.luvitをインストール

(上のリンク内のサイトにも書いてありますが、ダウンロード後にパスを通しましょう。)

2.作業したいディレクトリでdiscordiaをインストール

lit install SinisterRectus/discordia
  • 躓いたこと:シェルで上記のコマンドを実行したところ、pythonのモジュールであるlitが反応し、インストールできなかった。
  • 解決した方法:/home/user/binに入っているlitの名称をlualitに変更し、
lualit install SinisterRectus/discordia

とした。

discordiaでBOTを作る

discordのDeveloper PotalでBOTを作成します。

discordiaのwikiに詳しくBOTの作成方法が書いてあるため、自分のような初心者でもBOTが作成できます。
discordiaのwiki

自分はwikiに従って以下のようなbotを作成しました。

コード
local discordia = require('discordia')
local client = discordia.Client()

client:on('ready', function()
    print('Logged in as ' .. client.user.username)
end)

local function code(str)
    return string.format('```\n%s```', str)
end

local function log(str)
    local file = io.open("luabot.log", "a") -- append mode
    if file then
        file:write("[LOG] " .. str .. "\n")
        file:close()
    end
end

local function LoadAsciiArtFromFile()
    local file = io.open("asciiart.txt", "r")
    if file then
        local asciiArt = file:read("*a")
        file:close()
        return asciiArt
    end
end

local sandbox = setmetatable({
    os = {},
    io = {},
    debug = {},
    package = {}
}, {
    __index = _G
})

local function exec(arg, msg)

    if not arg then
        return
    end
    ---if msg.author ~= msg.client.owner then return end

    arg = arg:gsub('```\n?', '') -- strip markdown codeblocks

    local lines = {} -- this is where our printed lines will collect

    sandbox.message = msg

    sandbox.print = function(...) -- intercept printed lines with this
        local ret = {}
        for i = 1, select('#', ...) do
            local arg = tostring(select(i, ...))
            table.insert(ret, arg)
        end
        local line = table.concat(ret, '\t')
        log(line) -- logging
        table.insert(lines, table.concat(ret, '\t'))
    end

    local fn, syntaxError = load(arg, 'DiscordBot', 't', sandbox)
    if not fn then
        return msg:reply(code(syntaxError))
    end

    local success, runtimeError = pcall(fn) -- run the code
    if not success then
        return msg:reply(code(runtimeError))
    end -- handle runtime errors

    lines = table.concat(lines, '\n')

    if #lines > 1990 then -- truncate long messages
        lines = lines:sub(1, 1990)
    end

    return msg:reply(code(lines))

end

client:on('messageCreate', function(message)
    if message.content == '!ping' then
        message.channel:send('Pong!')
    end
    if message.content == '!love' then
        local asciiArt = LoadAsciiArtFromFile()
        message.channel:send(asciiArt)
    end
    -- Only process messages starting with "!lua"
    if message.content:sub(1, 4) ~= '!lua' then
        return
    end

    -- Extract the code from the message
    local code = message.content:sub(5)

    -- Execute the code
    exec(code, message)
end)

client:run('Bot MyBotToken')

(MyBotTokenには自分のbotのトークンが入ります。)

このコードをluabot.luaとして保存し、

luvit luabot.lua

で実行しました(後にsystemctlでデーモン化した)。
BOTをDiscord Develper PortalのOAuth2URL GeneratorからURLを発行し、サーバーに招待しました。
その後、discord内で権限(Permission)を付与しました。

  • 躓いたところ: discordのDMではBOTは反応するのに対し、サーバーのチャンネルでは反応しなかった。

DMで反応するところ:
スクリーンショット (773).png

サーバーのチャンネルで反応しないところ:
スクリーンショット (774).png

  • 解決した方法:

Discord Developer Portalで以下の画像のように設定を変更したら、反応するようになった。
スクリーンショット (775).png

サーバーのチャンネルでも反応するところ:

スクリーンショット (776).png

終わりに

分からないことやご指摘がありましたら、コメントお願いします。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?