@maksimenkomax

Как отправить с БД (django) значение и отправить сообщением в дискорд через бота?

Хочу отправить некоторые значения с бд сообщением в дискорд, выдает ошибку:
spoiler

Ignoring exception in command bot_info:
Traceback (most recent call last):
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "A:\wwz\discord_controle\management\commands\console.py", line 30, in bot_info
i = str(check_bot())
File "A:\wwz\discord_controle\management\commands\console.py", line 21, in check_bot
for object in bot_info_check.objects.all().order_by('id'):
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 287, in __iter__
self._fetch_all()
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\query.py", line 53, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\db\models\sql\compiler.py", line 1154, in execute_sql
cursor = self.connection.cursor()
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\django\utils\asyncio.py", line 24, in inner
raise SynchronousOnlyOperation(message)
django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 903, in invoke
await ctx.command.invoke(ctx)
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 859, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "C:\Users\Максим\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async.

Вот сам код
# -*- coding: utf8 -**-
from django.core.management.base import BaseCommand
from wwzapp.models import *
from discord_controle.models import *
from django.contrib.auth.models import User


class Command(BaseCommand):
    help = 'console'

    def handle(self, *args, **kwargs):
        from discord_controle.models import bot_info_check
        import discord
        from discord.ext import commands
        from discord_controle.management.commands.config import settings

        bot_discord = commands.Bot(command_prefix=settings['prefix'])

        def check_bot():
            name_bot = ""
            for object in bot_info_check.objects.all().order_by('id'):
                name_bot = name_bot + object.name + "\n"

            print(name_bot)

        @bot_discord.command(pass_context=True) # You need to allow to pass the Context object to the command function
        async def bot_info(ctx):
            author = ctx.message.author
            if author.id == 468806418787467274:
                i = str(check_bot())
                await ctx.send(i)
            else:
                print(ctx.message.author.id)
                await ctx.send('Недостаточно прав')

А вот бд
from django.db import models


class bot_info_check(models.Model):
    id = models.IntegerField(
        verbose_name='ID',
        primary_key=True
    )
    name = models.TextField(
        verbose_name='Название',
    )
  • Вопрос задан
  • 112 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы