Knowledge when making a bot using discord.py

I made a bot for managing the bot of Pricone, so I noticed that. https://github.com/izmktr/yukarisan

Events on_ready

on_ready is called when connecting to discord Also, it seems to be disconnected from discord irregularly, It will also be called when you reconnect automatically.

Here, it seems good to combine the information that you have internally with the data of discord.py.

import discord
from discord.ext import tasks
from typing import List, Dict, Optional

#guild_Linking id with internal class
guildhash: Optional[Dict[int, GuildData]] = None

@client.event
def on_ready():
global guildhash

if guildhash is None:
#Load processing
    guildhash = {}

for g in client.guilds:
    gdata = guildhash.get(g.id)
    if gdata is None:
        gdata = GuildData()
        guildhash[g.id] = gdata
    gdata.guild = g

Processing that accompanies the message

Occurs when adding / removing reactions, It is on_reaction_add, on_reaction_remove, but it may not be called. Generally, a message about 15 minutes after it is posted may not respond. (I think it depends on the activity of the server) Probably, I feel that on_message_delete does not fly when this happens.

Since max_messages is 1000, I thought it could be avoided by increasing it, I couldn't avoid it even if I put 100000 like this. (Looking for knowledge in this area)

#has no meaning?
client = discord.Client(max_messages = 100000)

You can use on_raw_reaction_add and on_raw_reaction_remove to make sure you fly. Save the message information with on_message and use on_raw_reaction_ ~ It would be nice to resolve the process.

In addition, it is called in the order of on_raw_reaction_add → on_reaction_add. If on_reaction_add flies here, I thought it would be on_raw_reaction_add if it didn't fly, This process seems to be troublesome because the order is reversed.

Private chat

You can have a private chat with the bot by sending a message to the bot. If you use development commands, use private chat, You can work without seeing the logs around you.

@client.event
async def on_message(message):
    #Ignore if the message sender is a bot
    if message.author.bot:
        return

    #Normal message
    if message.channel.type == discord.ChannelType.text:
        return

    # 1:1 message
    if message.channel.type == discord.ChannelType.private:
        return

The problem is that the type of message is different from that of a normal message So there are class variables and class functions that are missing.

Also, since there is no guild in the private message, You must specify the guild in your private message.

If you design it to absorb this difference and create processing for various commands It is a reflection that it would have been convenient.

Recommended Posts

Knowledge when making a bot using discord.py
The story of making a question box bot with discord.py
A memorandum when using beautiful soup
A addictive story when using tensorflow on Android
A template that I often use when making Discord BOT in Python (memorial note)
SoC FPGA: A small story when using on Linux
A swampy story when using firebase on AWS lamda
A memorandum when making a surveillance camera with Raspberry Pi
Summary when using Fabric
Precautions when using Chainer
Create a slack bot
I got a Value Error when using JUMAN ++ with PyKNP
A story that stumbled when using pip in a proxy environment
A memo when creating a directed graph using Graphviz in Python
Create a real-time auto-reply bot using the Twitter Streaming API
When I made a Discord Bot, my classmates destroyed my computer
Problems when using Elasticsearch as a data source in Redash
When using property, use a class that inherits object (new-style class)
Let's make a LINE bot using various services [ngrok edition]