{"id":68227,"date":"2022-10-29T09:00:31","date_gmt":"2022-10-29T09:00:31","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=68227"},"modified":"2022-10-29T09:00:31","modified_gmt":"2022-10-29T09:00:31","slug":"set-up-a-matrix-to-discord-bot","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=68227","title":{"rendered":"Set up a Matrix to Discord bot"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">Set up a Matrix to Discord bot<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/seth\" class=\"username\">Seth Kenlon<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Sat, 10\/29\/2022 &#8211; 03:00<\/span><\/p>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Matrix is a popular <a href=\"https:\/\/opensource.com\/alternatives\/slack\" target=\"_blank\" rel=\"noopener\">open source chat application<\/a> that makes it easy to chat securely with people all over the world. Similarly, Discord is a non-open source chat application that&#8217;s also popular with many online communities. Discord, like Matrix, provides a chat client for all major platforms both mobile and desktop, so it&#8217;s perfectly usable on Linux. However, it&#8217;s not open source and so, given the choice, you might prefer to use Matrix. The good news is that when not given a choice for any reason, you can <strong>also<\/strong> use Matrix to interface with Discord by running a Matrix-to-Discord bridge. This article shows you how to set up and run a Python Matrix bot to bridge chat between a Matrix room and a Discord channel.<\/p>\n<h2>Requirements<\/h2>\n<p>The bot demonstrated in this article is a &#8220;non-puppeting&#8221; bridge, meaning that it just copies ingoing and outgoing messages on one platform and sends it to the other. There are more advanced modes available, but those tend to require a self-hosted Matrix instance. The procedure for setting up the bot, however, is similar in both cases, so whether you&#8217;re setting up a bridge service for your self-hosted Matrix server or just a puppeting bot for public instances, I assume you have at least:<\/p>\n<ul>\n<li>\n<p>A Matrix account and a Matrix client such as <a href=\"https:\/\/element.io\/\">Element<\/a>.<\/p>\n<\/li>\n<li>\n<p>A Discord account.<\/p>\n<\/li>\n<li>\n<p>A Linux or BSD server that can run the Python3 bot. I use a <a href=\"https:\/\/opensource.com\/article\/19\/3\/netbsd-raspberry-pi\">Rev. 1 Raspberry Pi<\/a> with just a 700mHZ processor and 256 MB RAM, running NetBSD. You can run the bot locally, if you prefer, but I find it more convenient to run it as a persistent service so I don&#8217;t miss messages that happen while I&#8217;m away.<\/p>\n<\/li>\n<\/ul>\n<h2>Get the bot<\/h2>\n<p>Download or clone <a href=\"https:\/\/github.com\/git-bruh\/matrix-discord-bridge\">matrix-discord-bridge<\/a>.<\/p>\n<p>Change into its <code>bridge<\/code> directory and install its dependencies using <a href=\"https:\/\/opensource.com\/article\/19\/11\/python-pip-cheat-sheet\">pip<\/a>:<\/p>\n<pre class=\"highlight\">\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>python3 <span class=\"re5\">-m<\/span> pip <span class=\"kw2\">install<\/span> <span class=\"re5\">-r<\/span> requirements.txt<\/code><\/span><\/pre>\n<p>Run the bot to generate an empty configuration file:<\/p>\n<pre class=\"highlight\">\n<span class=\"geshifilter\"><code class=\"bash geshifilter-bash\"><span class=\"co4\">$ <\/span>python3 .<span class=\"sy0\">\/<\/span>bridge.py<\/code><\/span><\/pre>\n<p>You now have a file called <code>config.json<\/code> in your current directory. It contains six key and value pairs. The rest of this article demonstrates how to obtain these values, but first an overview:<\/p>\n<p>The top three are for Matrix.<\/p>\n<ul>\n<li>\n<p><strong>homeserver<\/strong>: The Matrix server you log in to<\/p>\n<\/li>\n<li>\n<p><strong>username<\/strong>: Your Matrix login name<\/p>\n<\/li>\n<li>\n<p><strong>password<\/strong>: Your Matrix password<\/p>\n<\/li>\n<\/ul>\n<p>Two are for Discord:<\/p>\n<ul>\n<li>\n<p><strong>token<\/strong>: A bot developer token obtained from Discord.<\/p>\n<\/li>\n<li>\n<p><strong>discord_cmd_prefix<\/strong>: A character sequence you want to use as a shortcut for sending the bot commands through Discord.<\/p>\n<\/li>\n<\/ul>\n<p>And the final one is for both:<\/p>\n<ul>\n<li>\n<p><strong>bridge<\/strong>: The Discord &#8220;channel&#8221; ID and the Matrix &#8220;room&#8221; ID that you&#8217;re bridging. This can contain more than one channel and room pair, so you can use just one bot to bridge several rooms.<\/p>\n<\/li>\n<\/ul>\n<h2>Set up Matrix<\/h2>\n<p>All you have to do to set up the Matrix side is open a Matrix account for your bot.<\/p>\n<p>Next, you need the ID of the room you want to bridge to Discord. To get a room ID, right-click on the room icon in the left panel of Element and select <strong>Copy Link<\/strong>. In the URL you&#8217;ve just copied, there&#8217;s a semicolon. The room ID is the part on left of the semicolon, and the home server of that room is to the right. For example, suppose this is the URL you just copied:<\/p>\n<pre class=\"highlight\">\n<span class=\"geshifilter\"><code class=\"text geshifilter-text\">https:\/\/matrix.to\/#\/!DEADBEEFzzzzABCDEF:matrix.org?via=matrix.org<\/code><\/span><\/pre>\n<p>The room ID is <code>!DEADBEEFzzzzABCDEF<\/code> and the home server is <code>matrix.org<\/code>.<\/p>\n<p>You can now add your Matrix details to the <code>config.json<\/code> file. For example:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-json\" data-lang=\"json\">    \"homeserver\": \"https:\/\/matrix.org\",\n    \"username\": \"@mybot:matrix.org\",\n    \"password\": \"myBadPassword1234\",\n    \"token\": \"\",\n    \"discord_cmd_prefix\": \"\",\n    \"bridge\": {\n        \"\": \"!DEADBEEFzzzzABCDEF:matrix.org\"\n    }\n}\n----<\/code><\/pre>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More Linux resources<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/linux-commands-cheat-sheet\/?intcmp=70160000000h1jYAAQ\">Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/developers.redhat.com\/cheat-sheets\/advanced-linux-commands\/?intcmp=70160000000h1jYAAQ\">Advanced Linux commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.redhat.com\/en\/services\/training\/rh024-red-hat-linux-technical-overview?intcmp=70160000000h1jYAAQ\">Free online course: RHEL technical overview<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-networking?intcmp=70160000000h1jYAAQ\">Linux networking cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/cheat-sheet-selinux?intcmp=70160000000h1jYAAQ\">SELinux cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/linux-common-commands-cheat-sheet?intcmp=70160000000h1jYAAQ\">Linux common commands cheat sheet<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/what-are-linux-containers?intcmp=70160000000h1jYAAQ\">What are Linux containers?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/linux?intcmp=70160000000h1jYAAQ\">Our latest Linux articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2>Get a Discord token<\/h2>\n<p>Assuming you already have an account on Discord, open a web browser and navigate to <a href=\"https:\/\/discordapp.com\/developers\/applications\/\">discordapp.com\/developers\/applications<\/a>. Once you&#8217;ve logged in, click the <strong>New Application<\/strong> button in the <strong>Applications<\/strong> tab.<\/p>\n<p>Give your bot a name. For this example, I use <code>mybot<\/code>.<\/p>\n<p>After you&#8217;ve defined a bot, click on it and find the <strong>Bot<\/strong> category in the menu on the left.<\/p>\n<p>In the <strong>Bot<\/strong> panel, click the <strong>Add Bot<\/strong> button. Discord adds your bot to the panel, alerting you that &#8220;A wild bot has appeared!&#8221; in a message box. Under the name of your bot, there&#8217;s a link to click to reveal your bot&#8217;s token. Click the link and copy the token into your config file.<\/p>\n<pre class=\"highlight\">\n<span class=\"geshifilter\"><code class=\"text geshifilter-text\">\"token\": \"07c63.fb2823cG759.b20_852f337a6551bc\",<\/code><\/span><\/pre>\n<h2>Set the bot command<\/h2>\n<p>Choose a sequence of characters you want to use to issue commands to the bot in Discord. In the instance of a simple bridge, you may not have any commands you need to issue, so this value probably doesn&#8217;t actually matter. I set it to <code>!b<\/code> but I&#8217;ve never used it.<\/p>\n<pre class=\"highlight\">\n<span class=\"geshifilter\"><code class=\"text geshifilter-text\">\"discord_cmd_prefix\": \"!b\",<\/code><\/span><\/pre>\n<h2>Add your bot to Discord<\/h2>\n<p>Now you must add your bot to the channel you want it to bridge.<\/p>\n<ol class=\"arabic\">\n<li>\n<p>Select <strong>OAuth2<\/strong> from the menu on the left, and then <strong>URL Generator<\/strong>.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/opensource.com\/sites\/default\/files\/2022-10\/discord-oauth2.webp\" width=\"413\" height=\"382\" alt=\"Select URL Generator under the OAuth2 menu item.\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Seth Kenlon, CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<\/li>\n<li>\n<p>In the <strong>Scopes<\/strong> section, select <strong>bot<\/strong> (and only <strong>bot<\/strong>). In the <strong>Bot Permissions<\/strong> section that appears under the <strong>Scopes<\/strong> section, activate all options under <strong>Text Permissions<\/strong>.<\/p>\n<\/li>\n<li>\n<p>Copy the URL displayed at the bottom of the panel, in the <strong>Generated URL<\/strong> field.<\/p>\n<\/li>\n<\/ol>\n<p>Navigate to the URL you just copied, and add the bot to the channel.<\/p>\n<p>You&#8217;re done with the Discord web interface, but now there&#8217;s one more configuration option you need from the Discord app.<\/p>\n<h2>Get the Discord channel ID<\/h2>\n<p>In <strong>User Settings<\/strong> of Discord (it&#8217;s the gear icon next to your name on the desktop app), select <strong>Advanced<\/strong>. In the <strong>Advanced<\/strong> panel, activate <strong>Developer Mode<\/strong>.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/opensource.com\/sites\/default\/files\/2022-10\/discord-developer-mode.webp\" width=\"927\" height=\"389\" alt=\"Activate Developer Mode with the toggle switch in the top right corner of the Advanced panel.\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Seth Kenlon, CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<p>With developer mode active, go to the channel you want to bridge. For instance, you might want to bridge Matrix to the <strong>zombie apocalypse<\/strong> channel on the <strong>example<\/strong> Discord server. First, join the <strong>example<\/strong> Discord server. Then right-click on text channel <strong>#zombie apocalypse<\/strong>, and select <strong>Copy ID<\/strong>.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/opensource.com\/sites\/default\/files\/2022-10\/discord-channel-id.webp\" width=\"1200\" height=\"675\" alt=\"Copy ID is at the bottom of the context menu\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Seth Kenlon, CC BY-SA 4.0<\/p>\n<\/div>\n<\/article>\n<p>Paste the channel ID into the config file as the first value for <strong>bridge<\/strong>. Your full config file now contains:<\/p>\n<pre class=\"highlight\">\n<code class=\"language-json\" data-lang=\"json\">    \"homeserver\": \"https:\/\/matrix.org\",\n    \"username\": \"@mybot:matrix.org\",\n    \"password\": \"myBadPassword1234\",\n    \"token\": \"07c63.fb2823cG759.b20_852f337a6551bc\",\n    \"discord_cmd_prefix\": \"!b\",\n    \"bridge\": {\n        \"1030287944604463185\": \"!DEADBEEFzzzzABCDEF:matrix.org\"\n    }\n}<\/code><\/pre>\n<h2>Bridge<\/h2>\n<p>Your bot is now acting as a bridge, at least in theory. Some Discord channels start new users in a muted state, and they must be granted special permissions to interact. If the Discord channel you&#8217;re bridging is strictly managed, then you may need to speak to a moderator or administrator and request special permissions for your bot.<\/p>\n<p>For your first attempt at setting this up, it&#8217;s easiest to bridge a Discord server to a Matrix room that you control. That way, you can confirm that it works when unrestricted. After you&#8217;ve confirmed functionality, try adding it to a restricted channel.<\/p>\n<h2>Open source bridges another gap<\/h2>\n<p>Open source does a lot of heavy lifting, including in the integration space. It&#8217;s to the credit of both Matrix and Discord that they provide a robust bot ecosystem that&#8217;s easy to learn and easy to use. It&#8217;s to the credit of some resourceful open source developers that the two can be bridged.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Run a Python Matrix bot to bridge chat between a Matrix room and a Discord channel.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/10\/talk_chat_communication_team.png\" width=\"520\" height=\"292\" alt=\"Chat bubbles\" title=\"Chat bubbles\"><\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-tags field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/tools\" hreflang=\"en\">Tools<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/python\" hreflang=\"en\">Python<\/a><\/div>\n<\/p><\/div>\n<div class=\"hidden field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/10\/cc-by-sa--32.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?destination=\/feed&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Set up a Matrix to Discord bot Seth Kenlon Sat, 10\/29\/2022 &#8211; 03:00 Matrix is a popular open source chat application that makes it easy to chat securely with people all over the world. Similarly, Discord is a non-open source chat application that&#8217;s also popular with many online communities. Discord, like Matrix, provides a chat [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":68228,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-68227","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68227","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=68227"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/68227\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/68228"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=68227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=68227"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=68227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}