[PYTHON] [Discord.py] A solution for those who cannot successfully introduce disperser

Introduction

I'm new to python. I started learning from discord.py. As the title says, for those who don't like the dispander in discord.py I've shared a fix that worked well in my environment. It may be wrong, so I would appreciate it if you could report it in the comments.

Postscript: 2020/10/04 14:30 This code has a good chance of making a mistake in my environment. If it doesn't work, think of it as one solution. I can't tell if this method is correct. As you said in the comments, I think it's code that works normally. However, it is released based on the fact that it did not work with the Github code comitt 789d88f shown at the end.

Postscript: 2020/10/04/22: 50 __ I have confirmed that it works with the code provided by a certain person. It is posted in the last part, so please have a look there. __

Development environment

discord.py: 1.5.0 Heroku dispander: 0.4.0

code

Below is the code for ** when used as a function ** introduced in dispander by DiscordBotPortalJP.

root/  ├ dispander/  │ ├ __init__.py  │ └ module.py  └ discordbot.py

samplebot.py


import discord
from dispander import dispand
client = discord.Client()

@client.event
async def on_message(message):
    if message.author.bot:
        return
    await dispand(message)

client.run(token)

However, it didn't work in my environment. ~~ Probably, the dispander's cog setup function is not called. ~~


So I changed it to the code below.

root/  ├ dispander/  │ └ module.py  └ discordbot.py

discord.py(Modified version)


import discord
import dispander.module as dispand

bot = discord.Client()
@bot.event
async def on_ready():
    dispand.setup(bot)
    await dispand(message)

bot.run(token)

I wrote that ...

root/  ├ dispander/  │ └ module.py  └ discordbot.py

Someone provided the following code. When I checked it in my environment, I confirmed that it works normally, so I posted it.

discord.py(updated version)


import os
import re
import discord
from dispander.module import dispand
from discord.ext import commands
client = discord.Client()

@client.event
async def on_message(message):
    if message.author.bot:
        return
    await dispand(message)

client.run(token)

Commentary

From ~~ Try using Cog of discord.py (Rewrite version), use cog setup function I made the call as dispand.setup (bot). Also, if it is ʻon_message (message):, there is a problem that other functions do not work, so it is called in ʻon_ready ():. ~~ dispander

Finally

Dispanser Invitation Link. Code is available on github. Thank you for reading until the end! If you have any mistakes, I mentioned them at the beginning, but I would appreciate it if you could write them in the comments.

Recommended Posts

[Discord.py] A solution for those who cannot successfully introduce disperser
Anxible points for those who want to introduce Ansible
PyPI registration steps for those who want to make a PyPI debut
A memo for those who want quick socket communication with netcat
A memo for those who use Python in Visual Studio (me)
AWS ~ For those who will use it ~