答案:
所以我从下面的问题中做了一些其他的事情,我实际上没有使用 .JSON
文件并尝试了这个:
Prefixes.py(齿轮文件):
@commands.Cog.listener()
async def on_message(self, message):
mention = f'<@!{self.client.user.id}>'
if mention in message.content:
prefix = await self.client.get_prefix(message)
try:
x = ""
pfp = self.client.user.avatar_url
prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`.", color = 0x456383)
prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
await message.channel.send(embed = prefix)
except discord.Forbidden:
return await message.channel.send(f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`")
我所做的是,我添加了 prefix = await self.client.get_prefix(message)
。这将从 .JSON
文件中获取前缀,或者如果 .JSON
文件为空,它将使用可以在 /
文件中找到的默认前缀 Main.py
。这对我来说很好,对你来说也应该很好。可以在此处找到我的主文件中的内容:
Main.py:
def get_prefix(client, message):
with open('prefixes.json', 'r') as file:
prefixes = json.load(file)
if str(message.guild.id) in prefixes:
return prefixes[str(message.guild.id)]
else:
return "/"
client = commands.Bot(command_prefix = get_prefix)
================================================ ==============================
问题:
所以我制作了一个 on_message
事件,所以每当有人提到客户端的 ID 时,它都会回复一条消息,其中包含服务器前缀。 (我使用的是 per-server-prefix
系统。)所以我尝试了这个:
@commands.Cog.listener()
async def on_message(self, message):
def botprefix(self, message):
with open('prefixes.json', 'r') as file:
prefixes = json.load(file)
if str(message.guild.id) in prefixes:
return prefixes[str(message.guild.id)]
else:
return "/"
mention = f'<@!{self.client.user.id}>'
if mention in message.content:
try:
x = ""
pfp = self.client.user.avatar_url
prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{botprefix}`. If you want to find out more information about me type `{botprefix}help`.", color = 0x456383)
prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
await message.channel.send(embed = prefix)
except discord.Forbidden:
return await message.channel.send(f"My prefix for **{message.guild.name}** is `{botprefix}`. If you want to find out more information about me type `{botprefix}help`")
谁能解释为什么这不起作用以及我如何解决它?
答案 0 :(得分:1)
您正在使用一个函数来生成前缀,这意味着您需要先调用它才能实际获取前缀。这是您需要进行的更改:
@commands.Cog.listener()
async def on_message(self, message):
def botprefix(self, message):
with open('prefixes.json', 'r') as file:
prefixes = json.load(file)
if str(message.guild.id) in prefixes:
return prefixes[str(message.guild.id)]
else:
return "/"
mention = f'<@!{self.client.user.id}>'
if mention in message.content:
prefix = botprefix(message) # Calculate the bot prefix for this guild
try:
x = ""
pfp = self.client.user.avatar_url
prefix = discord.Embed(title=x, description=f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`.", color = 0x456383)
prefix.set_footer(text=f"ChizLanks", icon_url=pfp)
await message.channel.send(embed = prefix)
except discord.Forbidden:
return await message.channel.send(f"My prefix for **{message.guild.name}** is `{prefix}`. If you want to find out more information about me type `{prefix}help`")
我将 botprefix(message)
的结果存储在 prefix
中,然后在 f 字符串中使用它。
我上面所做的是对您的代码进行快速而肮脏的修复。 但是,您应该对您的机器人进行一些更改。首先,您应该将 botprefix()
移出 on_message
函数,然后在声明您的机器人时,将其用作 command_prefix
参数,如下所示:
def get_prefix(bot, message):
with open('prefixes.json', 'r') as file:
prefixes = json.load(file)
if str(message.guild.id) in prefixes:
return prefixes[str(message.guild.id)]
else:
return "/"
bot = commands.Bot(command_prefix=get_prefix)
您可以找到有关计算动态前缀 here 的更多信息。