0

Хотелось бы чтобы в один момент параллельно работало несколько обработчиков process_command. Пробовал через threading, ругается ибо функция async. asyncio.run бросает TypeError: run() argument after * must be an iterable, not NoneType. Есть ли возможность реализовать параллельное выполнение?

Код:

import discord
from discord.ext import commands
import time

token="token"
prefix = "s!"
owner_id=0
# ----------



print("Загрузка...")

bot = commands.Bot(command_prefix=prefix)

@bot.event
async def on_ready():
    print("Бот типа готов")

@bot.event
async def on_message(message):

    if message.content.lower().startswith(prefix):
        await process_command(message.content[len(prefix):].split(' '), message.channel, message.author)

    if message.author.id!=owner_id:
        return

    if message.content=='<@%s>'%bot.user.id:
        await message.channel.send('Сейчас в сети.')

async def process_command(args, channel, author):
    args[0]=args[0].lower()
    if author.id!=owner_id:
        return
    if args[0]=="say":
        await bot.get_channel(int(args[1])).send(" ".join(args[2:]))
    elif args[0]=="em":
        await bot.get_channel(int(args[1])).send(bot.get_emoji(int(args[2])))
    elif args[0]=="status":
        await bot.change_presence(activity=discord.Game(name=" ".join(args[2:]), type=int(args[1])))
    elif args[0]=="sleep":
        time.sleep(int(args[1]))

if __name__=='__main__':
    bot.run(token)

1 ответ 1

1

Расширение commands используется намного проще.

Посмотрите примеры кода на GitHub репозитории библиотеки.

Не используйте time.sleep в асинхронном коде. time.sleep - блокирующая функция. Используйте asyncio.sleep

import discord
from discord.ext import commands
import random

PREFIX = "s!"

bot = commands.Bot(command_prefix=PREFIX)

@bot.event
async def on_ready():
    print('Logged in as')
    print(bot.user.name)
    print(bot.user.id)
    print('------')

@bot.command()
async def say(ctx, *, text: str):
    await ctx.send(text)

bot.run('token')
1
  • Спасибо, вот оказывается где была проблема.
    – user399810
    28 авг 2020 в 0:23

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.