Overview
It's very common to see that some discord groups add a discord bot. What does discord bot can do for us? It can reply to user depends on what they send。 Let's say when our users send some sad words the bot can encourage she/he by sending some words. Furthermore, when a new member joined our group, the bot can welcome the newcomers because the owner maybe not always online. The bot is scalable you can use it to create something awesome.
library
After searching GitHub i found out several discord Api libraries.
- Cog-Creators/Red-DiscordBot(python)
- discordjs/discord.js(javascript)
- Rapptz/discord.py(python)
- bwmarrin/discordgo(golang) I choose Rapptz/discord.py in this piece, because i damn love python。
Install env
First and foremost, we need to install our python env. We need to install python 3.8 or higher version. If you don't wanna to install any local env, that'sthat's ok you can use some cloud env like replit.com or pythonanywhere whatever you want. But for me i installed env using anaconda software. It provides UI and it's quite easy to use。
get a bot token
For sure we need a token。 Then how do we get it? Just click "https://discord.com/developers/applications" and then login your discord account. You will see the new application button near your avatar. After creating an application go to bot page and copy your token. You should be very careful not to leak it。
invent bot to our chat group
I hope you have a free discord chat group already。 If not, you can create one for free。Go to our application page again, you will see Oauth2/URL Generator。 Generate an invite link for your application by picking the scopes and permissions it needs to function。 Get the URL and open it in our browser。That's all。
Some awesome python code
import discord
class MyClient(discord.Client):
async def on_ready(self):
print('Logged on as', self.user)
async def on_message(self, message):
# don't respond to ourselves
if message.author == self.user:
return
if message.content == 'ping':
await message.channel.send('pong')
intents = discord.Intents.default()
intents.message_content = True
client = MyClient(intents=intents)
client.run('YOUR TOKEN HERE')
If you need a proxy you can attach some code like this
client = MyClient(intents=intents, proxy="http://127.0.0.1:10809")
This proxy address is my local proxy address. You should replace it with your own address if you wanna use a proxy.
result
After you run python demo.py you will see
2022-09-30 10:15:48 INFO discord.client logging in using static token
2022-09-30 10:15:50 INFO discord.gateway Shard ID None has connected to Gateway (Session ID: xxxxxxx).
Logged on as xxxx-demo#4518
After all of above is done you will see your bot is online。
If you get any question about discord bot or discord application please feel free to connect me.
Top comments (0)