MrCocoDev / django_discord.py

MIT License
7 stars 0 forks source link

AttributeError: 'NoneType' object has no attribute 'send' #1

Closed mwmeyer closed 1 year ago

mwmeyer commented 1 year ago

I am experiencing an error when trying to configure django_discord with an existing django app:

python manage.py runserver
/venv/lib/python3.9/site-packages/django_discord/py/bot/apps.py", line 27, in ready
    async_to_sync(channel_layer.send)(
AttributeError: 'NoneType' object has no attribute 'send'

In my projects settings.py, i've set: DISCORD_BOT_PATH = 'myproject.myapp.discord_bot'

Which is just a simple example, which work when running manually.

import discord
from discord.ext import commands

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='>', intents=intents)

async def start_bot(token):
    @bot.event
    async def on_message(message):
        print(message.author, message.content, bot.user)

        if message.author == bot.user:
            return

        if message.content.startswith('>ping'):
            await message.channel.send('pong')

    bot.run(token)

The README is a little light on details regarding on how to configure an existing django project, so I am unsure whether there is a setting i've missed, or whether its my bot file.

MrCocoDev commented 1 year ago

Hi there! Sorry for the delay, I didn't see this until this morning. I'll take a look ASAP.

MrCocoDev commented 1 year ago

@mwmeyer , can you add this to your asgi.py file?

consumers_module = importlib.import_module('django_discord.py.bot.consumers')

application = ProtocolTypeRouter(
    {
        "http": django_asgi_app,
        "channel": ChannelNameRouter(
            {
                "discord_bot": consumers_module.BaseDiscordConsumer.as_asgi(),
            }
        ),
    }
)

and follow the docs here https://channels.readthedocs.io/en/stable/installation.html to setup Django for async support? If you can't run in ASGI let me know and I'll add a management command to run discord.py without channels support.

mwmeyer commented 1 year ago

thanks!