python - Discord Client Bot 没有以正确的方式响应?不和谐.py
问题描述
回答:
所以我从下面的问题中做了其他事情,实际上我没有使用.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
文件中找到的默认前缀。这对我来说很好,对你也应该很好。我在主文件中的内容可以在这里找到:
主要.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`")
谁能解释为什么这不起作用以及我该如何解决?
解决方案
您正在使用一个函数来生成前缀,这意味着您需要先调用它才能实际获取前缀。这是您需要进行的更改:
@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)
inprefix
然后在 f-strings 中使用它的结果。
读我
我上面所做的是对您的代码的快速而肮脏的修复。但是,您应该对您的机器人进行一些更改。首先,您应该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)
您可以在此处找到有关计算动态前缀的更多信息。
推荐阅读
- javascript - 如何在 webview 中隐藏网站的元素
- android - 意图过滤器在 Android Lollipop 上不起作用
- mql4 - MQL4 挂单错误 # 130
- php - 如何覆盖 Laravel 包的 JSON 翻译?
- airflow - 获取有关 Airflow on_failure_callback 上下文的异常详细信息
- mongodb - 在 kubernetes 中运行 mongodb - azure aks 失败
- object-recognition - 在预加载的点云上重新定位智能手机
- javascript - 以编程方式访问 JavaScript 中的字体规范
- javascript - 将多个数组合并为单个数组
- c++ - 函数返回的未来