Science and technology

Set up a Matrix to Discord bot

Matrix is a well-liked open source chat application that makes it straightforward to talk securely with individuals all around the world. Similarly, Discord is a non-open supply chat software that is additionally widespread with many on-line communities. Discord, like Matrix, offers a chat consumer for all main platforms each cellular and desktop, so it is completely usable on Linux. However, it isn’t open supply and so, given the selection, you would possibly desire to make use of Matrix. The excellent news is that when not given a selection for any purpose, you’ll be able to additionally use Matrix to interface with Discord by working a Matrix-to-Discord bridge. This article reveals you easy methods to arrange and run a Python Matrix bot to bridge chat between a Matrix room and a Discord channel.

Requirements

The bot demonstrated on this article is a “non-puppeting” bridge, which means that it simply copies ingoing and outgoing messages on one platform and sends it to the opposite. There are extra superior modes obtainable, however these are inclined to require a self-hosted Matrix occasion. The process for organising the bot, nevertheless, is comparable in each circumstances, so whether or not you are organising a bridge service on your self-hosted Matrix server or only a puppeting bot for public situations, I assume you’ve gotten not less than:

  • A Matrix account and a Matrix consumer resembling Element.

  • A Discord account.

  • A Linux or BSD server that may run the Python3 bot. I exploit a Rev. 1 Raspberry Pi with only a 700mHZ processor and 256 MB RAM, working NetBSD. You can run the bot domestically, if you happen to desire, however I discover it extra handy to run it as a persistent service so I do not miss messages that occur whereas I’m away.

Get the bot

Download or clone matrix-discord-bridge.

Change into its bridge listing and set up its dependencies utilizing pip:

$ python3 -m pip set up -r necessities.txt

Run the bot to generate an empty configuration file:

$ python3 ./bridge.py

You now have a file known as config.json in your present listing. It incorporates six key and worth pairs. The remainder of this text demonstrates easy methods to get hold of these values, however first an summary:

The prime three are for Matrix.

  • homeserver: The Matrix server you log in to

  • username: Your Matrix login identify

  • password: Your Matrix password

Two are for Discord:

  • token: A bot developer token obtained from Discord.

  • discord_cmd_prefix: A personality sequence you need to use as a shortcut for sending the bot instructions by way of Discord.

And the ultimate one is for each:

Set up Matrix

All it’s a must to do to arrange the Matrix facet is open a Matrix account on your bot.

Next, you want the ID of the room you need to bridge to Discord. To get a room ID, right-click on the room icon within the left panel of Element and choose Copy Link. In the URL you’ve got simply copied, there is a semicolon. The room ID is the half on left of the semicolon, and the house server of that room is to the precise. For instance, suppose that is the URL you simply copied:

https://matrix.to/#/!DEADBEEFzzzzABCDEF:matrix.org?via=matrix.org

The room ID is !DEADBEEFzzzzABCDEF and the house server is matrix.org.

You can now add your Matrix particulars to the config.json file. For instance:

    "homeserver": "https://matrix.org",
    "username": "@mybot:matrix.org",
    "password": "myBadPassword1234",
    "token": "",
    "discord_cmd_prefix": "",
    "bridge": {
        "": "!DEADBEEFzzzzABCDEF:matrix.org"
    }
}
----

Get a Discord token

Assuming you have already got an account on Discord, open an internet browser and navigate to discordapp.com/developers/applications. Once you’ve got logged in, click on the New Application button within the Applications tab.

Give your bot a reputation. For this instance, I exploit mybot.

After you’ve got outlined a bot, click on on it and discover the Bot class within the menu on the left.

In the Bot panel, click on the Add Bot button. Discord provides your bot to the panel, alerting you that “A wild bot has appeared!” in a message field. Under the identify of your bot, there is a hyperlink to click on to disclose your bot’s token. Click the hyperlink and replica the token into your config file.

"token": "07c63.fb2823cG759.b20_852f337a6551bc",

Set the bot command

Choose a sequence of characters you need to use to concern instructions to the bot in Discord. In the occasion of a easy bridge, it’s possible you’ll not have any instructions it is advisable to concern, so this worth most likely does not really matter. I set it to !b however I’ve by no means used it.

"discord_cmd_prefix": "!b",

Add your bot to Discord

Now you have to add your bot to the channel you need it to bridge.

  1. Select OAuth2 from the menu on the left, after which URL Generator.

    Seth Kenlon, CC BY-SA 4.0

  2. In the Scopes part, choose bot (and solely bot). In the Bot Permissions part that seems underneath the Scopes part, activate all choices underneath Text Permissions.

  3. Copy the URL displayed on the backside of the panel, within the Generated URL discipline.

Navigate to the URL you simply copied, and add the bot to the channel.

You’re completed with the Discord net interface, however now there’s another configuration possibility you want from the Discord app.

Get the Discord channel ID

In User Settings of Discord (it is the gear icon subsequent to your identify on the desktop app), choose Advanced. In the Advanced panel, activate Developer Mode.

Seth Kenlon, CC BY-SA 4.0

With developer mode energetic, go to the channel you need to bridge. For occasion, you would possibly need to bridge Matrix to the zombie apocalypse channel on the instance Discord server. First, be part of the instance Discord server. Then right-click on textual content channel #zombie apocalypse, and choose Copy ID.

Seth Kenlon, CC BY-SA 4.0

Paste the channel ID into the config file as the primary worth for bridge. Your full config file now incorporates:

    "homeserver": "https://matrix.org",
    "username": "@mybot:matrix.org",
    "password": "myBadPassword1234",
    "token": "07c63.fb2823cG759.b20_852f337a6551bc",
    "discord_cmd_prefix": "!b",
    "bridge": {
        "1030287944604463185": "!DEADBEEFzzzzABCDEF:matrix.org"
    }
}

Bridge

Your bot is now appearing as a bridge, not less than in concept. Some Discord channels begin new customers in a muted state, and so they have to be granted particular permissions to work together. If the Discord channel you are bridging is strictly managed, then it’s possible you’ll want to talk to a moderator or administrator and request particular permissions on your bot.

For your first try at setting this up, it is best to bridge a Discord server to a Matrix room that you just management. That manner, you’ll be able to affirm that it really works when unrestricted. After you’ve got confirmed performance, strive including it to a restricted channel.

Open supply bridges one other hole

Open supply does loads of heavy lifting, together with within the integration area. It’s to the credit score of each Matrix and Discord that they supply a strong bot ecosystem that is straightforward to study and simple to make use of. It’s to the credit score of some resourceful open supply builders that the 2 may be bridged.

Most Popular

To Top