Write-Only Discord Bots Are Surprisingly Easy to Code in Python

Write-Only Discord Bots Are Surprisingly Easy to Code in Python Featured Image

Early on in the summer, I had this idea to post all of my Reddit upvotes to a Discord channel since I was already sharing them regularly. Of course, once I realized how easy this was to do, I immediately coded up half a dozen bots without a second thought. Now, I’m wondering why I was so scared to give it a try before. As a result, I wanted to write a post to encourage others to at least code up a write-only bot in their free time.

Table of Contents

What Is a Write-Only Discord Bot?

For the purpose of this article, I’m going to assume you know what Discord is. That said, the quick summary is that it’s a group chat app.

One of the cool things about Discord is that you can write code to perform various functions for you. We call these programs bots, and they can be used to do just about anything. For example, I used to use a bot to post to The Renegade Coder Discord whenever a release was made on one of our GitHub repos. More recently, I’ve been using bots to update folks whenever I release an article as well as manage tasks like granting access to chat rooms.

In the world of Discord, there are actually two different types of bots: write-only and general purpose. A write-only bot can only post messages. In contrast, a general purpose bot can do anything a human can do like read messages and change server settings.

If you’re like me, you were probably imagining that a general purpose bot was the only kind of bot, and that’s what makes them so daunting. Personally, I didn’t want to have to deal with the logistics of writing code to listen for events like user messages.

Fortunately, there’s an alternative that is extremely manageable: write-only bots. And, you can write one yourself in less than 5 lines of code. Don’t believe me? Keep reading!

Setting Up a Write-Only Discord Bot

Before you can write any code for your write-only Discord bot, you need to get some information from Discord. This can be a bit of a pain with general purpose bots because you need to setup permissions and whatnot. That’s not the case with a write-only bot.

To create a write-only bot, you can click on the cog symbol for any one of your channels (FYI: write-only bots can only exist in one channel at a time):

From there, you can select the “Integrations” option from the left sidebar:

There, you should see some options. The option you’re looking for is “Webhooks”:

Chances are this is your first time creating a webhook. That’s fine! Go ahead and click “Create Webhook”. It should create one for you as follows:

And, you’re done! At this point, you can rename the webhook and provide a profile picture. These will show up as your bots name and profile picture.

Otherwise, if this is good enough for you, feel free to click “Copy Webhook URL” and move on to the coding.

How to Code a Write-Only Bot

Now that you have a webhook URL, the coding portion is pretty straightforward. First, you’ll what to create an instance of your bot as follows:

import discord
webhook = discord.Webhook.from_url("<your webhook url>", adapter=discord.RequestsWebhookAdapter())

Chances are at this point your code won’t run. The reason for that is discord library is third party. No worries! Make sure you run some for of pip to install it (e.g., pip install discord).

With that out of the way, we’ve created our first bot. All that’s left is to have the bot send messages to the channel. We can do that using the send() method:

import discord
webhook = discord.Webhook.from_url("<your webhook url>", adapter=discord.RequestsWebhookAdapter())
webhook.send("Hello, World!")

And, that’s it! Your bot just sent “Hello, World!” to the channel that your setup the webhook in. Now, imagine the possibilities.

Imagine the Possibilities

Despite only being able to send messages with this bot, there’s actually a lot you could do. For example, as I mentioned already, you could scrape Reddit and post your findings on Discord. Here’s what that code might look like:

webhook = discord.Webhook.from_url("<your webhook url>", adapter=discord.RequestsWebhookAdapter())
submission = reddit.random_subreddit().random()
webhook.send(submission.url)

In this example, the reddit object is an authenticated Reddit instance which I pull a random post from a randum subreddit. Then, I send a URL for that post directly which gets rendered by Discord. For fun, I schedule a script like this to run every hour just to check out new random Reddit content.

That said, why stop at Reddit? I’m sure there are APIs out there for all kinds of services like YouTube and Twitter where you can pull content regularly. Or, you could even write your own scraper! For example, you might current a list of RSS feeds and post their content to your Discord regularly.

Ultimately, whatever you choose to do with your write-only Discord bot, feel free to let me know how it turns out! I sure there are some truly hilarious bots you could be making (or, you can make something useful…).

With that said, that’s all I have for today! If you liked this article and want to learn more Python as a result, you’re in the right place. Here are some related Python articles:

Likewise, here are some resources from the folks at Amazon (#ad):

Otherwise, take care!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Code Posts