Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中生成if/then行_Python_Discord - Fatal编程技术网

如何在python中生成if/then行

如何在python中生成if/then行,python,discord,Python,Discord,我的discord机器人检测到一个单词,并发送角色id以ping该角色。我想创建一个if-then语句,这样如果它检测到的消息不包含某个单词,它就会转到下一行并在脚本中查找下一个单词。我使用replit作为bot的主机,因此keep_alive import keep_alive中的行是正常运行时间bot,代码末尾的20行是隐藏的discord auth令牌。我试图在两个触发字之间添加if/then/advance行 import discord import os client = disco

我的discord机器人检测到一个单词,并发送角色id以ping该角色。我想创建一个if-then语句,这样如果它检测到的消息不包含某个单词,它就会转到下一行并在脚本中查找下一个单词。我使用replit作为bot的主机,因此keep_alive import keep_alive中的行是正常运行时间bot,代码末尾的20行是隐藏的discord auth令牌。我试图在两个触发字之间添加if/then/advance行

import discord
import os
client = discord.Client()

from keep_alive import keep_alive

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith('Trigger Word'):
        await message.channel.send('<@&Role ID>')
    

@client.event
async def on_message(message):
    if message.author == client.user:
      return    
   
    if message.content.startswith('Random Trigger Word'):
        await message.channel.send('<@&Role ID>')

keep_alive()
client.run(os.environ['20'])

在这个上下文中,您不能这样做,因为两个块都是独立的协程

你可以调整你的条件,这样只有一个可能得到满足

首先: 如果message.content.starts带有触发字和\ 非message.content.starts随机触发字: 做那件事 ... 二号 如果message.content.starts使用随机触发字和\ 非message.content.startswithTrigger字: 做那件事 或者如果您有触发器->角色的映射

some_mapping={TriggerWord1:role1, 触发器2:角色2, TriggerWord3:role3} @客户端事件 _message上的异步定义消息: 如果message.author==client.user: 回来 对于触发器,角色在某些_mapping.items中: 如果message.startswithtrigger: 等待message.channel.sendf
@PatrickParker我不确定你在光顾提问者之前读过这个问题。他们写了一个糟糕的标题,但问题文本清楚地描述了他们试图做什么,而你的评论与之无关。我可能误解了你的问题。但自从亚当发现这一点很清楚,我就删除了我的评论。p、 如果我这样做,我得到的是,我将不得不添加多个单词并使其递归。有没有可能拥有一个单词数据库,如果它检测到特定的单词,它会返回与该单词关联的角色?@DiscordWelcomeBot-sure!我将构造一个{trigger word:role}字典,然后迭代该dict.items并为我的dict.items中的trigger_word、关联的_role执行操作:if message.content.startswithtrigger_word:wait message.channel.sendfI尝试了这一操作,但我无法运行代码。有没有一个具体的说法?我试着把它像其他的代码一样分开。启动时使用随机\u触发器\u单词输入wait message.channel.sendrole\u id,但它仍然无法工作。