Search code examples
pythonpython-3.xdiscordchatbot

Discord bot in python bug: RuntimeWarning: coroutine 'handle_response' was never awaited


I'm making a Discord bot and got an error. These are my files' code:

main.py

import bot


if __name__ == '__main__':
    # Run the bot
    bot.run_discord_bot()

bot.py

import discord
import responses


async def send_message(message, user_message, is_private):
    try:
        response = responses.handle_response(user_message)
        await message.author.send(response) if is_private else await message.channel.send(response)
    except Exception as e:
        print(e)


def run_discord_bot():
    bot_token = TOKEN
    intents = discord.Intents.default()
    intents.message_content = True
    client = discord.Client(intents=intents)

    @client.event
    async def on_ready():
        print(f"{client.user} is now running!")

    @client.event
    async def on_message(message):
        if message.author == client.user:
            return

        username = str(message.author)
        user_message = str(message.content)
        channel = str(message.channel)


        print(f"{username} said: `{user_message}` in {channel}")

        if user_message[0] == "!":
            user_message = user_message[1:]
            await send_message(message, user_message, is_private=True)
        else:
            await send_message(message, user_message, is_private=False)

    client.run(bot_token)

responses.py

import random
import discord


intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def handle_response(message) -> str:
    com_channel = CHANNEL ID
    hu_channel = CHANNEL ID
    de_channel = CHANNEL ID
    p_message = message.lower()

    username = str(message.author)
    user_message = str(message.content)
    channel = str(message.channel)

    # English
    greetings = ["Hello There! 😃", f"hi!", "Hello 👋", f"Hey!"]
    if p_message == "hello" or p_message == "hi" or p_message in greetings \
            and channel != hu_channel and channel != de_channel:
        return greetings[random.randint(0, len(greetings))]

    if p_message == "roll" or p_message == "roll a dice" or p_message == "roll dice" \
            and channel != hu_channel and channel != de_channel:
        return str(random.randint(1, 6))

    if p_message == "help" \
            and channel != hu_channel and channel != de_channel:
        return "`If you need help, feel free to @Mention the mods!`"

    farewells = ["Bye! 👋", "Ciao!"]
    if p_message == "bye" or p_message in farewells \
            and channel != hu_channel and channel != de_channel:
        return farewells[random.randint(0, len(farewells))]

    # Magyar
    koszonesek = ["Szia! 😃", f"Üdv!", "Hello 👋"]
    if p_message == "hello" or p_message == "szia" or p_message == "sziasztok" or p_message == "csá" or \
        p_message in koszonesek \
            and channel != com_channel and channel != de_channel:
        return koszonesek[random.randint(0, len(koszonesek))]

    if p_message == "dobj egy kockát" \
            and channel != com_channel and channel != de_channel:
        return str(random.randint(1, 6))

    if p_message == "segítség" \
            and channel != com_channel and channel != de_channel:
        return "`Hogyha segítségre lenne szükséged, nyugodtan @Említsd meg a mod-okat!`"

    if p_message == "megyek" or p_message == "én most megyek" \
            and channel != com_channel and channel != de_channel:
        return "Szia! 👋"

    # Deutsch
    begrussungen = ["Moin! 😃", f"Hi!", "Hallo 👋"]
    if p_message == "hallo" or p_message == "moin" or p_message == "hi" or p_message in begrussungen \
            and channel != com_channel and channel != hu_channel:
        return begrussungen[random.randint(0, len(begrussungen))]

    if p_message == "roll ein würfel" or p_message == "rolle einen würfel" or p_message == "roll" \
            or p_message == "würfel" \
            and channel != com_channel and channel != hu_channel:
        return str(random.randint(1, 6))

    if p_message == "hilfe" \
            and channel != com_channel and channel != hu_channel:
        return "`Fals du Hilfe brauchst @Erwähne die Mods!`"

    if p_message == "ich geh" or p_message == "ich gehe jetzt" \
            and channel != com_channel and channel != hu_channel:
        return "Tschüß! 👋"

This is the error in the console, if i type in "hi" on discord:

2023-05-09 10:57:51 INFO     discord.client logging in using static token
2023-05-09 10:57:52 INFO     discord.gateway Shard ID None has connected to Gateway (Session ID: 72b086a0a3abc6a43cf540c88089c8d1).
GrafBot#0573 is now running!
Graf 72#1007 said: `hi` in other1-💬
C:\Users\betty\PycharmProjects\GrafBot\bot.py:39: RuntimeWarning: coroutine 'handle_response' was never awaited
  await send_message(message, user_message, is_private=False)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

everything worked until I tried to determine from which channel and which message it could respond to.

I only edited this part:

intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)


@client.event
async def handle_response(message) -> str:
    com_channel = 935085549029564427
    hu_channel = 935079195242037259
    de_channel = 935084427283947550
    p_message = message.lower()

    username = str(message.author)
    user_message = str(message.content)
    channel = str(message.channel)

Ps.: Sorry for my bad english!


Solution

  • I think the error is in the send_message function. You need to await handle_response.

    async def send_message(message, user_message, is_private):
        try:
            response = await responses.handle_response(user_message)
            await message.author.send(response) if is_private else await message.channel.send(response)
        except Exception as e:
            print(e)