discord.py - 即使我希望机器人为一个人发送它,捐赠命令也会为所有流媒体发送相同的图片 | 不和谐.py
问题描述
我做了一个捐赠命令。但是,我想让它变得更好。
我在想这个:
- 当 Carson 是被捐赠的人时,机器人应该只用图像响应。目前,它捐赠给所有流媒体
- 机器人应该只拿钱,而不是说话。
@commands.command()
async def donate(self, ctx):
await ctx.send("Aight, Who do you want to donate to?")
streamer = await self.bot.wait_for('message', check=lambda message : message.author == ctx.author)
await ctx.send(f"Ok, so you are donating to {streamer.content}. How much money you gonna donate?")
amount = await self.bot.wait_for('message', check=lambda message : message.author == ctx.author)
await ctx.send(f"Aight, you are donating ${amount.content} to {streamer.content}. What's the donatation message?")
donation = await self.bot.wait_for('message', check=lambda message : message.author == ctx.author)
await ctx.send(f"You have just donated ${amount.content} to {streamer.content} with the message: {donation.content}")
if streamer.content == "CallMeCarson" or "callmecarson" or "carson":
await ctx.send(file=discord.File("./Images/callmecarsoncrying.jpg"))
return```
Im new to python, So sorry if this is very easy to do.
解决方案
问题在以下行:
if streamer.content == "CallMeCarson" or "callmecarson" or "carson":
这与以下含义相同:
if streamer.content == "CallMeCarson" or string == True or string == True:
python 中的非空字符串是truthy
,所以它总是True
& 传递,所以它总是适用于每个流媒体。
在 python 中使用and
oror
时,您需要重复您尝试比较的参数。
if streamer.content == "CallMeCarson" or streamer.content == "callmecarson" or streamer.content == "carson":
但这已经过时了,因为您可以将其转换为小写并检查是否carson
在其中:
if "carson" in streamer.content.lower():
假设您计划为多个流媒体执行此操作,我建议您制作一个dict
以避免 900 个 if 语句。您以后可以将所有其他名称和图像路径添加到此 dict 中。
streamers = {"carson": "callmecarsoncrying", "streamer2": "streamer2_image_name"}
for key in streamers:
if key in streamer.content.lower():
return await ctx.send(file=discord.File(f"./Images/{streamers[key]}.jpg"))
最后一行将发送消息,然后在找到匹配项时break
退出循环(by returning
),因此不会检查其他流媒体。这样,代码看起来更干净且更易于维护。
推荐阅读
- facebook - Detectron 2 Facebook 库和更新 Cocodaset
- evaluation - 成员访问(点)运算符中的评估(c 编程)
- python - 将数据框列值扩展到额外的行
- python - 如何计算字典中出现的连续小时数
- craftcms - 如何更改 CraftCMS 3 类别 uri 名称
- r - 如何过滤向量文件并返回一个新向量,该向量仅包含 R 上的“.html”
- java - java - 如何为java数组列表中的特定数字分配分数?
- r - 正则表达式,用于在 dyplr 工作流程中替换字符串中的非数字字符 INSIDE 括号
- jenkins - Jenkins 声明性管道因 NullPointerException 而失败
- python - FiPy:如何在两个网格之间的界面处找到节点(顶点)