diff --git a/README.md b/README.md index 6e9885ce..67c1373e 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,8 @@ Using this playbook, you can get the following services configured on your serve - (optional) the [matrix-appservice-irc](https://github.com/TeDomum/matrix-appservice-irc) bridge for bridging your Matrix server to [IRC](https://wikipedia.org/wiki/Internet_Relay_Chat) +- (optional) the [matrix-appservice-discord](https://github.com/Half-Shot/matrix-appservice-discord) bridge for bridging your Matrix server to [Discord](https://discordapp.com/) + Basically, this playbook aims to get you up-and-running with all the basic necessities around Matrix, without you having to do anything else. **Note**: the list above is exhaustive. It includes optional or even some advanced components that you will most likely not need. @@ -114,6 +116,8 @@ This playbook sets up your server using the following Docker images: - [tedomum/matrix-appservice-irc](https://hub.docker.com/r/tedomum/matrix-appservice-irc/) - the [matrix-appservice-irc](https://github.com/TeDomum/matrix-appservice-irc) bridge to [IRC](https://wikipedia.org/wiki/Internet_Relay_Chat) (optional) +- [tedomum/matrix-appservice-discord](https://hub.docker.com/r/halfshot/matrix-appservice-discord) - the [matrix-appservice-discord](https://github.com/Half-Shot/matrix-appservice-discord) bridge to [Discord](https://discordapp.com/) + ## Deficiencies diff --git a/docs/configuring-playbook-bridge-appservice-discord.md b/docs/configuring-playbook-bridge-appservice-discord.md new file mode 100644 index 00000000..ab47187e --- /dev/null +++ b/docs/configuring-playbook-bridge-appservice-discord.md @@ -0,0 +1,20 @@ +# Setting up Appservice Discord (optional) + +The playbook can install and configure [matrix-appservice-discord](https://github.com/Half-Shot/matrix-appservice-discord) for you. + +See the project's [documentation](https://github.com/Half-Shot/matrix-appservice-discord/blob/master/README.md) to learn what it does and why it might be useful to you. + +Setup Instructions: + +loosely based on [this](https://github.com/Half-Shot/matrix-appservice-discord#setting-up) + +1. Create a Discord Application [here](https://discordapp.com/developers/applications/me/create). +2. Retrieve Client ID and Bot token from this Application. +3. Enable the bridge with `matrix_appservice_discord_enabled: true` in your vars.yml and provide id and token. +4. Rerun playbook with "setup-all" tag. Restart with tag "start" afterwards. +5. Retrieve Discord invitelink from `{{ matrix_appservice_discord_base_path }}/invite_link` (this defaults to `/matrix/appservice-discord/invite_link`) +6. Invite the Bot to Discord servers you wish to bridge. Administrator permission is recommended. +7. Join the rooms by following this syntax `#_discord_guildid_channelid` + - can be easily retrieved by logging into Discord in a browser and opening the desired channel. URL will have this format: discordapp.com/channels/guild_id/channel_id + +Other configuration options are available via the `matrix_appservice_discord_configuration_extension_yaml` variable. \ No newline at end of file diff --git a/docs/configuring-playbook.md b/docs/configuring-playbook.md index 9a61c153..6285d4d1 100644 --- a/docs/configuring-playbook.md +++ b/docs/configuring-playbook.md @@ -52,3 +52,5 @@ When you're done with all the configuration you'd like to do, continue with [Ins - [Setting up Mautrix Whatsapp bridging](configuring-playbook-bridge-mautrix-whatsapp.md) (optional) - [Setting up Appservice IRC bridging](configuring-playbook-bridge-appservice-irc.md) (optional) + +- [Setting up Appservice Discord bridging](configuring-playbook-bridge-appservice-discord.md) (optional) diff --git a/group_vars/matrix-servers b/matrix-servers similarity index 95% rename from group_vars/matrix-servers rename to matrix-servers index 49183e37..e3337bf2 100644 --- a/group_vars/matrix-servers +++ b/matrix-servers @@ -20,6 +20,18 @@ # matrix-appservice-irc's client-server port to the local host (`127.0.0.1:9999`). matrix_appservice_irc_container_expose_client_server_api_port: "{{ not matrix_nginx_proxy_enabled }}" +###################################################################### +# +# matrix-appservice-discord +# +###################################################################### + +# Normally, matrix-nginx-proxy is enabled and nginx can reach matrix-appservice-discord over the container network. +# If matrix-nginx-proxy is not enabled, or you otherwise have a need for it, you can expose +# matrix-appservice-discord's client-server port to the local host (`127.0.0.1:9005`). +matrix_appservice_discord_container_expose_client_server_api_port: "{{ not matrix_nginx_proxy_enabled }}" + + ###################################################################### # # matrix-base diff --git a/roles/matrix-synapse/defaults/main.yml b/roles/matrix-synapse/defaults/main.yml index cbabaa6d..919f3d6c 100644 --- a/roles/matrix-synapse/defaults/main.yml +++ b/roles/matrix-synapse/defaults/main.yml @@ -18,6 +18,9 @@ matrix_synapse_container_expose_federation_api_port: false # Controls whether the Appservice IRC container exposes the Client/Server API port (tcp/9999). matrix_appservice_irc_container_expose_client_server_api_port: false +# Controls whether the Appservice Discord container exposes the Client/Server API port (tcp/9005). +matrix_appservice_discord_container_expose_client_server_api_port: false + # Controls whether the matrix-synapse container exposes the metrics port (tcp/9100). matrix_synapse_container_expose_metrics_port: false @@ -641,3 +644,124 @@ matrix_appservice_irc_configuration_extension_yaml: | matrix_appservice_irc_configuration_extension: "{{ matrix_appservice_irc_configuration_extension_yaml|from_yaml if matrix_appservice_irc_configuration_extension_yaml|from_yaml else {} }}" matrix_appservice_irc_configuration: "{{ matrix_appservice_irc_configuration_yaml|from_yaml|combine(matrix_appservice_irc_configuration_extension, recursive=True) }}" + + +# Matrix Appservice Discord is a Matrix <-> Discord bridge +# Enable Discord bridge +matrix_appservice_discord_enabled: false + +matrix_appservice_discord_docker_image: "halfshot/matrix-appservice-discord:latest" + +matrix_appservice_discord_base_path: "{{ matrix_base_data_path }}/appservice-discord" + +matrix_appservice_discord_client_id: "YOUR DISCORD APP CLIENT ID" +matrix_appservice_discord_bot_token: "YOUR DISCORD APP BOT TOKEN" + +matrix_appservice_discord_configuration_yaml: | + bridge: + domain: "{{ matrix_domain }}" + homeserverUrl: "{{ matrix_homeserver_url }}" + auth: + clientID: "{{matrix_appservice_discord_client_id}}" + botToken: "{{matrix_appservice_discord_bot_token}}" + database: + filename: "/data/discord.db" + userStorePath: "/data/user-store.db" + roomStorePath: "/data/room-store.db" + +matrix_appservice_discord_configuration_extension_yaml: | + # This is a sample of the config file showing all avaliable options. + # Where possible we have documented what they do, and all values are the + # default values. + # + #bridge: + # # Domain part of the bridge, e.g. matrix.org + # domain: "localhost" + # # This should be your publically facing URL because Discord may use it to + # # fetch media from the media store. + # homeserverUrl: "http://localhost:8008" + # # Interval at which to process users in the 'presence queue'. If you have + # # 5 users, one user will be processed every 500 milliseconds according to the + # # value below. This has a minimum value of 250. + # # WARNING: This has a high chance of spamming the homeserver with presence + # # updates since it will send one each time somebody changes state or is online. + # presenceInterval: 500 + # # Disable setting presence for 'ghost users' which means Discord users on Matrix + # # will not be shown as away or online. + # disablePresence: false + # # Disable sending typing notifications when somebody on Discord types. + # disableTypingNotifications: false + # # Disable deleting messages on Discord if a message is redacted on Matrix. + # disableDeletionForwarding: false + # # Enable users to bridge rooms using !discord commands. See + # # https://t2bot.io/discord for instructions. + # enableSelfServiceBridging: false + # # Disable sending of read receipts for Matrix events which have been + # # successfully bridged to Discord. + # disableReadReceipts: false + # Authentication configuration for the discord bot. + #auth: + # clientID: "12345" + # botToken: "foobar" + #logging: + # # What level should the logger output to the console at. + # console: "warn" #silly, verbose, info, http, warn, error, silent + # lineDateFormat: "MMM-D HH:mm:ss.SSS" # This is in moment.js format + # files: + # - file: "debug.log" + # disable: + # - "PresenceHandler" # Will not capture presence logging + # - file: "warn.log" # Will capture warnings + # level: "warn" + # - file: "botlogs.log" # Will capture logs from DiscordBot + # level: "info" + # enable: + # - "DiscordBot" + #database: + # userStorePath: "user-store.db" + # roomStorePath: "room-store.db" + # # You may either use SQLite or Postgresql for the bridge database, which contains + # # important mappings for events and user puppeting configurations. + # # Use the filename option for SQLite, or connString for Postgresql. + # # If you are migrating, see https://github.com/Half-Shot/matrix-appservice-discord/blob/master/docs/howto.md#migrate-to-postgres-from-sqlite + # # WARNING: You will almost certainly be fine with sqlite unless your bridge + # # is in heavy demand and you suffer from IO slowness. + # filename: "discord.db" + # # connString: "postgresql://user:password@localhost/database_name" + #room: + # # Set the default visibility of alias rooms, defaults to "public". + # # One of: "public", "private" + # defaultVisibility: "public" + #channel: + # # Pattern of the name given to bridged rooms. + # # Can use :guild for the guild name and :name for the channel name. + # namePattern: "[Discord] :guild :name" + # # Changes made to rooms when a channel is deleted. + # deleteOptions: + # # Prefix the room name with a string. + # #namePrefix: "[Deleted]" + # # Prefix the room topic with a string. + # #topicPrefix: "This room has been deleted" + # # Disable people from talking in the room by raising the event PL to 50 + # disableMessaging: false + # # Remove the discord alias from the room. + # unsetRoomAlias: true + # # Remove the room from the directory. + # unlistFromDirectory: true + # # Set the room to be unavaliable for joining without an invite. + # setInviteOnly: true + # # Make all the discord users leave the room. + # ghostsLeave: true + #limits: + # # Delay in milliseconds between discord users joining a room. + # roomGhostJoinDelay: 6000 + # # Delay in milliseconds before sending messages to discord to avoid echos. + # # (Copies of a sent message may arrive from discord before we've + # # fininished handling it, causing us to echo it back to the room) + # discordSendDelay: 750 + +matrix_appservice_discord_configuration_extension: "{{ matrix_appservice_irc_configuration_extension_yaml|from_yaml if matrix_appservice_irc_configuration_extension_yaml|from_yaml else {} }}" + +matrix_appservice_discord_configuration: "{{ matrix_appservice_discord_configuration_yaml|from_yaml|combine(matrix_appservice_discord_configuration_extension, recursive=True) }}" + + diff --git a/roles/matrix-synapse/tasks/ext/appservice-discord/init.yml b/roles/matrix-synapse/tasks/ext/appservice-discord/init.yml new file mode 100644 index 00000000..ee6fe1ff --- /dev/null +++ b/roles/matrix-synapse/tasks/ext/appservice-discord/init.yml @@ -0,0 +1,3 @@ +- set_fact: + matrix_systemd_services_list: "{{ matrix_systemd_services_list + ['matrix-appservice-discord'] }}" + when: matrix_appservice_discord_enabled diff --git a/roles/matrix-synapse/tasks/ext/appservice-discord/setup.yml b/roles/matrix-synapse/tasks/ext/appservice-discord/setup.yml new file mode 100644 index 00000000..9efdd457 --- /dev/null +++ b/roles/matrix-synapse/tasks/ext/appservice-discord/setup.yml @@ -0,0 +1,79 @@ +--- +- name: Ensure Appservice Discord image is pulled + docker_image: + name: "{{ matrix_appservice_discord_docker_image }}" + when: "matrix_appservice_discord_enabled" + +- name: Ensure Appservice Discord configuration path exists + file: + path: "{{ matrix_appservice_discord_base_path }}" + state: directory + mode: 0750 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_appservice_discord_enabled" + +- name: Ensure Matrix Appservice Discord config installed + copy: + content: "{{ matrix_appservice_discord_configuration|to_nice_yaml }}" + dest: "{{ matrix_appservice_discord_base_path }}/config.yaml" + mode: 0644 + owner: "{{ matrix_user_username }}" + group: "{{ matrix_user_username }}" + when: "matrix_appservice_discord_enabled" + +- name: Ensure matrix-appservice-discord.service installed + template: + src: "{{ role_path }}/templates/ext/appservice-discord/systemd/matrix-appservice-discord.service.j2" + dest: "/etc/systemd/system/matrix-appservice-discord.service" + mode: 0644 + register: matrix_appservice_discord_systemd_service_result + when: "matrix_appservice_discord_enabled" + +- name: Ensure systemd reloaded after matrix-appservice-discord.service installation + service: + daemon_reload: yes + when: "matrix_appservice_discord_enabled and matrix_appservice_discord_systemd_service_result.changed" + +- stat: + path: "{{ matrix_appservice_discord_base_path }}/discord-registration.yaml" + register: appservice_discord_registration_file + +- name: Generate matrix-appservice-discord discord-registration.yaml if it doesn't exist + shell: /usr/bin/docker run --rm --name matrix-appservice-discord-gen -v {{ matrix_appservice_discord_base_path }}:/data:z {{ matrix_appservice_discord_docker_image }} node build/src/discordas.js -r -u "http://matrix-appservice-discord:9005" -c /data/config.yaml -f /data/discord-registration.yaml -l discord_bot + when: "matrix_appservice_discord_enabled and appservice_discord_registration_file.stat.exists == False" + +- set_fact: + matrix_synapse_app_service_config_file_appservice_discord: '{{ matrix_appservice_discord_base_path }}/discord-registration.yml' + +- stat: + path: "{{ matrix_appservice_discord_base_path }}/invite_link" + register: appservice_discord_link_generated + +- name: Generate your discord invite link invite link + shell: /usr/bin/docker run --rm --name matrix-appservice-discord-link-gen -v {{ matrix_appservice_discord_base_path }}:/data -w /data {{ matrix_appservice_discord_docker_image }} /bin/sh -c "node .././build/tools/addbot.js > invite_link" + when: "matrix_appservice_discord_enabled and appservice_discord_link_generated.stat.exists == False" + +- set_fact: + matrix_synapse_container_additional_volumes: > + {{ matrix_synapse_container_additional_volumes }} + + + {{ [{'src': '{{ matrix_appservice_discord_base_path }}/discord-registration.yaml', 'dst': '{{ matrix_synapse_app_service_config_file_appservice_discord }}', 'options': 'ro'}] }} + when: "matrix_appservice_discord_enabled" + +- set_fact: + matrix_synapse_app_service_config_files: > + {{ matrix_synapse_app_service_config_files }} + + + {{ ["{{ matrix_synapse_app_service_config_file_appservice_discord }}"] | to_nice_json }} + when: "matrix_appservice_discord_enabled" + +# +# Tasks related to getting rid of matrix-appservice-discord (if it was previously enabled) +# + +- name: Ensure matrix-appservice-discord.service doesn't exist + file: + path: "/etc/systemd/system/matrix-appservice-discord.service" + state: absent + when: "not matrix_appservice_discord_enabled" diff --git a/roles/matrix-synapse/tasks/ext/init.yml b/roles/matrix-synapse/tasks/ext/init.yml index 6023116f..aa19ef6e 100644 --- a/roles/matrix-synapse/tasks/ext/init.yml +++ b/roles/matrix-synapse/tasks/ext/init.yml @@ -5,3 +5,5 @@ - import_tasks: "{{ role_path }}/tasks/ext/mautrix-whatsapp/init.yml" - import_tasks: "{{ role_path }}/tasks/ext/appservice-irc/init.yml" + +- import_tasks: "{{ role_path }}/tasks/ext/appservice-discord/init.yml" diff --git a/roles/matrix-synapse/tasks/ext/setup.yml b/roles/matrix-synapse/tasks/ext/setup.yml index 609269e7..fe113281 100644 --- a/roles/matrix-synapse/tasks/ext/setup.yml +++ b/roles/matrix-synapse/tasks/ext/setup.yml @@ -11,3 +11,5 @@ - import_tasks: "{{ role_path }}/tasks/ext/mautrix-whatsapp/setup.yml" - import_tasks: "{{ role_path }}/tasks/ext/appservice-irc/setup.yml" + +- import_tasks: "{{ role_path }}/tasks/ext/appservice-discord/setup.yml" diff --git a/roles/matrix-synapse/templates/ext/appservice-discord/systemd/matrix-appservice-discord.service.j2 b/roles/matrix-synapse/templates/ext/appservice-discord/systemd/matrix-appservice-discord.service.j2 new file mode 100644 index 00000000..af91b185 --- /dev/null +++ b/roles/matrix-synapse/templates/ext/appservice-discord/systemd/matrix-appservice-discord.service.j2 @@ -0,0 +1,30 @@ +[Unit] +Description=Matrix Appservice Discord server +After=docker.service +Requires=docker.service +Requires=matrix-synapse.service +After=matrix-synapse.service + +[Service] +Type=simple +ExecStartPre=-/usr/bin/docker kill matrix-appservice-discord +ExecStartPre=-/usr/bin/docker rm matrix-appservice-discord +ExecStart=/usr/bin/docker run --rm --name matrix-appservice-discord \ + --log-driver=none \ + --user={{ matrix_user_uid }}:{{ matrix_user_gid }} \ + --cap-drop=ALL \ + --network={{ matrix_docker_network }} \ + {% if matrix_appservice_discord_container_expose_client_server_api_port %} + -p 127.0.0.1:9005:9005 \ + {% endif %} + -v {{ matrix_appservice_discord_base_path }}:/data \ + {{ matrix_appservice_discord_docker_image }} + + +ExecStop=-/usr/bin/docker kill matrix-appservice-discord +ExecStop=-/usr/bin/docker rm matrix-appservice-discord +Restart=always +RestartSec=30 + + [Install] +WantedBy=multi-user.target