from discord.ext import commands
import json
with open(r"C:UsersintelDesktoprijalbotrijaldatanarrators.json") as f:
narrators = json.load(f)
class cmds(commands.Cog):
def __init__(self, client):
self.bot = client
@commands.command()
async def rijal(self, ctx, message):
if narrators.keys() in ctx.message:
val = narrators.get(ctx.message)
if val == "weak":
await ctx.send("True")
叙述者.json
{"a": "weak"}
我想让我的discord bot检查我的消息是否包含json中的密钥,但每次我运行它并执行[!rijal a]命令时,它都不会执行任何操作,它应该发送"真">
narrators.keys()
是字典中所有键的视图。message
是一个字符串,因此narrators.keys()
永远不会在message
中。
如果消息不完全相同,narrators.get(message)
也不会工作。您使用的是in
,所以您只是在查找子字符串。例如:"a"
是"another"
中的,但{"a": "weak"}.get("another")
找不到匹配项,因为"another"
不在字典中。
如果需要子字符串,请在键上循环。要获取该值,请使用相应的key
而不是message
从dict中获取,因为这不起作用(如上所述)。另一种选择是在键&同时使用CCD_ 13。
如果您想要完全匹配,如果未找到任何内容,get()
将返回None
。
PS考虑使用实际数据库而不是JSON文件