DEV Community

Ayden
Ayden

Posted on

Create a cool discord bot for your chat group

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')
Enter fullscreen mode Exit fullscreen mode

If you need a proxy you can attach some code like this

client = MyClient(intents=intents, proxy="http://127.0.0.1:10809")
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay