2使用MongoDB和discord.py时出错

2024-10-04 11:35:02 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在努力在我的discord机器人上为每台服务器设置不同的前缀

所以我的数据库设置如下

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

这是我的主要任务

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

Tags: inpymessagegetprefixlocallineusers