2 Ошибка при работе с MongoDB и discord.py - PullRequest
0 голосов
/ 03 марта 2020

Так что я работал над тем, чтобы на моем диссон-боте работали разные префиксы для каждого сервера

, поэтому моя база данных настроена следующим образом

{
   "SERVER ID": "PREFIX"
   "SERVER ID": "PREFIX"
   "SERVER ID": "PREFIX"
}

И вот мой main.py

import discord
from discord.ext import commands
import json
import os
import pymongo
from pymongo import MongoClient

mongo = MongoClient('localhost', 27017)
db = mongo.dpy
prefixes = db.prefixes
prefix_query = prefixes.find()


def get_prefix(client, message):
    guildid = str(message.guild.id)

    for doc in prefix_query:
            print(doc[guildid])
            return doc[guildid]



client = commands.Bot(command_prefix=get_prefix)
client.remove_command('help')
client.owner_id = int('393165866285662208')

exts = []

for f in os.listdir('.\\commands'):
    if f.endswith('.py'):
        d = f.replace('.py','')
        exts.append(d)

@client.event
async def on_ready():
    print('Client is Ready!')
    print(f'logged in as {client.user}')



if __name__ == '__main__':

    for ext in exts:
        try:
            client.load_extension(f'commands.{ext}')
            print(ext)
        except Exception as error:
            print(f'[CONSOLE] {ext} CANT BE LOADED: {error}')


client.run('TOKEN')

Так что моя проблема в том, что когда я запускаю, она всегда работает с первой командой, но когда я запускаю вторую, она выдает мне эту ошибку

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
    await self.process_commands(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 926, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 842, in get_context
    prefix = await self.get_prefix(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 787, in get_prefix
    ret = await discord.utils.maybe_coroutine(prefix, self, message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\utils.py", line 317, in maybe_coroutine
    value = f(*args, **kwargs)
  File "client.py", line 18, in get_prefix
    print(doc[guildid])
KeyError: '575865854542217231'

Затем, когда я запускаю третью команду, я получаю другая ошибка

Ignoring exception in on_message
Traceback (most recent call last):
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 791, in get_prefix
    ret = list(ret)
TypeError: 'NoneType' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 312, in _run_event
    await coro(*args, **kwargs)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 930, in on_message
    await self.process_commands(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 926, in process_commands
    ctx = await self.get_context(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 842, in get_context
    prefix = await self.get_prefix(message)
  File "C:\Users\jackw\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 798, in get_prefix
    raise TypeError("command_prefix must be plain string, iterable of strings, or callable "
TypeError: command_prefix must be plain string, iterable of strings, or callable returning either of these, not NoneType
...