M
M
megared2020-09-06 17:14:37
Python
megared, 2020-09-06 17:14:37

Code freezes while waiting for messages in discord.py module, how to solve?

The bot is waiting for new messages in the line

@bot.event
    async def on_message(message):
        if message.content == ('qq'):
            channel = message.channel
            await channel.send('здарова')

And it stops responding to commands like "$ping" and so on, because it is busy waiting for new messages. How to decide? Here is the whole code:

import discord
import json
import requests
import asyncio
from discord.ext import commands
import discord.utils

TOKEN = 'токен'
bot = commands.Bot(command_prefix='$')  # инициализируем бота с префиксом '$'

try:
    print('started')
    @bot.event
    async def on_message(message):
        if message.content == ('qq'):
            channel = message.channel
            await channel.send('здарова')
    @bot.command() #разрешаем передавать агрументы
    async def пинг(ctx): #создаем асинхронную фунцию бота
        await ctx.send('Привет! Я бот Red!') #отправляем обратно аргумент
    @bot.command()
    async def роли(ctx):
        member = ctx.message.author
        member_roles = member.roles
        await ctx.send(f"{member.mention} список твоих ролей:\n{member_roles}")
    @bot.command()
    async def ищи(ctx, arg):
        response = requests.get('https://some-random-api.ml/img/' + str(arg.lower()))  # Get-запрос
        json_data = json.loads(response.text)  # Извлекаем JSON
        embed = discord.Embed(color=0xff9900, title='Изображение:')  # Создание Embed'a
        embed.set_image(url=json_data['link'])  # Устанавливаем картинку Embed'a
        await ctx.send(embed=embed)  # Отправляем Embed
    bot.run(TOKEN)
except Exception as exc:
    print(exc)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2020-09-06
@megared

This is a known issue, the answer is in the FAQ in the very first paragraph https://discordpy.readthedocs.io/en/latest/faq.htm...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question