我正在尝试创建一个 setprefix
命令,它会更改服务器前缀。但是,我收到以下错误:
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 coroutine
我的代码:
<块引用>主文件
#------importing packages
import keep_alive
import os
import discord
from discord.ext import commands
import pymongo
from pymongo import MongoClient
import asyncio
import motor
import motor.motor_asyncio
import prefixes
from prefixes import Prefixes
### MongoDB Variables ###
mongo_url = os.environ['Mongodb_url']
cluster = motor.motor_asyncio.AsyncIOMotorClient(str(mongo_url))
db = cluster['Database']
collection = db['prefixes']
bot = commands.Bot(command_prefix = Prefixes.setprefix(), case_insensitive=True)
bot.remove_command('help')
my_token = os.environ['Token']
#------When bot is online
@bot.event
async def on_ready():
#status
#playing game status
await bot.change_presence(activity=discord.Game(
name=f'On {len(bot.guilds)} Servers | -help'))
print('Bot is Ready')
initial_extensions = ['help', 'math1', 'mod', 'anime', 'prefixes']
if __name__ == '__main__':
for extension in initial_extensions:
bot.load_extension(extension)
#ping latency....
@bot.command()
async def ping(ctx):
await ctx.send(f'Pong\n{round(bot.latency * 1000)}ms')
#------Running the bot
keep_alive.keep_alive()
bot.run(my_token)
<块引用>
前缀齿轮文件
import discord
from discord.ext import commands
import asyncio
import motor
import motor.motor_asyncio
import os
class Prefixes(commands.Cog):
def __init__(self, bot):
self.bot = bot
#custom prefix
@commands.command()
@commands.has_permissions(administrator = True)
async def setprefix(self, ctx, prefix):
### MongoDB Variables ###
mongo_url = os.environ['Mongodb_url']
cluster = motor.motor_asyncio.AsyncIOMotorClient(str(mongo_url))
db = cluster['Database']
collection = db['prefixes']
guild_id = ctx.guild.id
server_prefix = prefix
if (await collection.count_documents({})) == 0:
prefix_info = {'GuildId': guild_id, 'Prefix': server_prefix}
await collection.insert_one(prefix_info)
else:
prefix_info = {'GuildId': guild_id, 'Prefix': server_prefix}
await collection.update_one({'GuildId': guild_id}, {'$set': {'Prefix': prefix}})
await ctx.send(f'My prefix is now {prefix}')
finding_prefix = await collection.find_one({'Prefix': {'$eq': prefix}})
view_finding_prefix = finding_prefix.values()
iterate_view = iter(view_finding_prefix)
first_value = next(iterate_view)
self.bot(command_prefix = str(first_value), case_insesitive = True)
def setup(bot):
bot.add_cog(Prefixes(bot))
我不明白为什么会有 TypeError
。我正在使用 motor
,因为它支持异步。在 motor documentation
中,它说当使用 find_one()
时,它作为字典给出。这就是为什么我这样做的原因,正如您在上面的代码中看到的:
finding_prefix = await collection.find_one({'Prefix': {'$eq': prefix}})
view_finding_prefix = finding_prefix.values()
iterate_view = iter(view_finding_prefix)
first_value = next(iterate_view)
self.bot(command_prefix = str(first_value), case_insesitive = True)
我这样做是为了获得键 Prefix
的第一个值。如果还有其他人可以这样做,请告诉我。
答案 0 :(得分:0)
我认为您需要一个数据库来制作自定义前缀代码,因为您需要为机器人所在的每个服务器自定义前缀。