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
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
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:
- 11 Python Practice Problems for Beginners
- Can You Actually Return Multiple Values From a Function in Python?
- Can We Fit Rock Paper Scissors in Python in a Tweet?
Likewise, here are some resources from the folks at Amazon (#ad):
- Effective Python: 90 Specific Ways to Write Better Python
- Python Tricks: A Buffet of Awesome Python Features
- Python Programming: An Introduction to Computer Science
Otherwise, take care!
Recent Code Posts
When it comes to organizing your logic in a program, one tool that can do a lot of heavy lifting for you is short-circuit evaluation. In this article, I'll give a little bit of background around...
Learning to write code in Python is a lot of fun, but you may find that life gets a lot harder when you start playing around with more complex projects. Let's talk Python's module and package system.