我发出了捐赠命令。但是,我想做得更好。
我在想这个:
@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.
答案 0 :(得分:0)
问题在以下行中:
UCHAR buf[64];
这与以下内容相同:
if streamer.content == "CallMeCarson" or "callmecarson" or "carson":
python中的非空字符串为if streamer.content == "CallMeCarson" or string == True or string == True:
,因此它将始终为truthy
并通过,因此它将始终适用于每个流媒体。
在python中使用True
或and
时,您需要重复尝试比较的参数。
or
但是这已经过时了,因为您可以将其转换为小写并检查if streamer.content == "CallMeCarson" or streamer.content == "callmecarson" or streamer.content == "carson":
是否在其中:
carson
假设您打算对多个流光进行此操作,建议您制作一个if "carson" in streamer.content.lower():
以避免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
,因此将不检查其他任何拖缆。通过这种方式,代码看起来更加整洁且易于维护。