Feature/discord plugin (#54)

* Add exp_discord plugin for Discord login and account linking

Players log in to the web interface with Discord through OAuth2 with the
identify scope, so no bot token is needed. After logging in they get a
code from the login page and type /link-discord <code> in game to link
that Factorio account. One Discord account can hold up to
exp_discord.max_accounts Factorio accounts (default 3), and linking a
name moves it off any other Discord account.

Sessions and link codes live in memory on the controller plugin, the
links are stored in database/exp_discord/accounts.json. The user page
shows the linked Discord account and lets the user, or anyone with
exp_discord.link.delete, unlink it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Link Discord from the user page and document the setup

The user page gets a Link Discord button. It asks the controller for a
one-time ticket bound to the logged in user, then goes through the
authorize route with it. The callback links the Discord account to that
user directly, without the in-game code, and goes back to the user page.
The link flow always shows the Discord consent screen.

The README covers registering the redirect URL and the config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: bbassie <17990055+bbassie@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-10-12 14:05:15 +09:00
1 parent 2dfbf970df
commit 0c593cd9fe
22 files changed
+1674

No files matched your search

+30
View File
@@ -0,0 +1,30 @@
--[[-- ExpDiscord - Commands
Commands which link and unlink the player's Discord account
]]
local Commands = require("modules/exp_commands")
local Discord = require("modules/exp_discord")
--- Links your account to the Discord account which made the code, printed once the controller answers
Commands.new("link-discord", { "exp-discord_commands.description-link" })
:optional("code", { "exp-discord_commands.arg-code" }, Commands.types.string)
:add_aliases{ "link" }
:register(function(player, code)
--- @cast code string?
if code then
Discord.link(player, code)
else
Discord.get_link(player)
end
end)
--- Removes the link to your Discord account, printed once the controller answers
Commands.new("unlink-discord", { "exp-discord_commands.description-unlink" })
:add_aliases{ "unlink" }
:register(function(player)
Discord.unlink(player)
end)
-- Access the exports from other modules using require("modules/exp_discord")
return require("modules/exp_discord/control")
+88
View File
@@ -0,0 +1,88 @@
--[[-- ExpDiscord
Links Factorio accounts to Discord accounts, the links are kept on the controller.
A player logs in with Discord on the web interface, which gives them a code.
Typing that code in game links the account they are playing on. Nothing is
stored in the lua state, every call goes to the controller and the answer is
printed to the player once it arrives, if they are still online.
]]
local clusterio_api = require("modules/clusterio/api")
local ExpUtil = require("modules/exp_util")
--- @class ExpDiscord
local ExpDiscord = {}
local error_settings = { color = ExpUtil.color.orange_red, sound_path = "utility/wire_pickup" }
--- Link the player to the Discord account which made the code
--- @param player LuaPlayer
--- @param code string
function ExpDiscord.link(player, code)
clusterio_api.send_json("exp_discord:link", { player_name = player.name, code = code })
end
--- Remove the link between the player and their Discord account
--- @param player LuaPlayer
function ExpDiscord.unlink(player)
clusterio_api.send_json("exp_discord:unlink", { player_name = player.name })
end
--- Print the Discord account the player is linked to
--- @param player LuaPlayer
function ExpDiscord.get_link(player)
clusterio_api.send_json("exp_discord:get", { player_name = player.name })
end
--- A player who can still be printed to, nil once they have left
--- @param name string
--- @return LuaPlayer?
local function get_online_player(name)
local player = game.get_player(name)
if player and player.valid and player.connected then
return player
end
return nil
end
--- The controller linked the player
--- @param payload { player_name: string, discord_name: string }
function ExpDiscord.receive_linked(payload)
local player = get_online_player(payload.player_name)
if not player then return end
player.print{ "exp-discord.linked", payload.discord_name }
end
--- The controller removed the link, discord_name is nil if there was none
--- @param payload { player_name: string, discord_name: string? }
function ExpDiscord.receive_unlinked(payload)
local player = get_online_player(payload.player_name)
if not player then return end
if payload.discord_name then
player.print{ "exp-discord.unlinked", payload.discord_name }
else
player.print({ "exp-discord.not-linked" }, error_settings)
end
end
--- The controller answered which account the player is linked to
--- @param payload { player_name: string, discord_name: string? }
function ExpDiscord.receive_get(payload)
local player = get_online_player(payload.player_name)
if not player then return end
if payload.discord_name then
player.print{ "exp-discord.linked-to", payload.discord_name }
else
player.print{ "exp-discord.not-linked" }
end
end
--- The controller refused a request
--- @param payload { player_name: string, message: string }
function ExpDiscord.receive_error(payload)
local player = get_online_player(payload.player_name)
if not player then return end
player.print(payload.message, error_settings)
end
return ExpDiscord
+10
View File
@@ -0,0 +1,10 @@
--[[
It is best practice to not expose any globals because all modules share a global environment
However, sometimes you need globals, for example to access functions within rcon commands
Therefore, we advise that this should be the only file in your module to expose globals
]]
--- @diagnostic disable: global-in-non-module
-- Access using `/sc exp_discord.foo()`
exp_discord = require("modules/exp_discord/control")
+10
View File
@@ -0,0 +1,10 @@
[exp-discord]
linked=Your account is now linked to the Discord account __1__.
linked-to=Your account is linked to the Discord account __1__. Use /unlink-discord to remove it.
unlinked=Your account is no longer linked to the Discord account __1__.
not-linked=Your account is not linked to a Discord account. Log in with Discord on the website to get a code, then use /link-discord <code>.
[exp-discord_commands]
description-link=Links your account to Discord using the code from the website, or shows the account you are linked to.
description-unlink=Removes the link between your account and Discord.
arg-code=Code shown on the website after logging in with Discord.
+14
View File
@@ -0,0 +1,14 @@
{
"name": "exp_discord",
"load": [
],
"require": [
"globals.lua",
"commands.lua"
],
"dependencies": {
"clusterio": "*",
"exp_util": "*",
"exp_commands": "*"
}
}
+3
View File
@@ -0,0 +1,3 @@
-- Access the exports from other modules using require("modules/exp_discord")
return require("modules/exp_discord/control")