首页 > 解决方案 > python中的elif语法无效

问题描述

我正在尝试学习如何使用 python,我一直想制作自己的 discrod 机器人,所以我尝试使用 discord.py 但我添加了 elif 来使它更加优化,所以如果没有命令通过但正确使用前缀,然后通过帮助消息,但不知何故我的 elif 语法错误,这是我的代码,错误在第 33 行,如果有差异,我使用 replit

import discord
import os
import random
from random import randint

client = discord.Client()

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

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

    if message.content.startswith('%%help'):
      embedVar = discord.Embed(title="Commands", description="Fine here's a list of commands", color=0x00ff00)
      embedVar.add_field(name="%%coin", value="randomly says heads or tails", inline=True)
      embedVar.add_field(name="%%messagedev", value="sends me a message that I unfortanatly cannot repond to", inline=True)
      embedVar.add_field(name="%%die @exaple", value="puts a funny message with whoever you @ dying", inline=True)
      embedVar.add_field(name="%%wrk", value="randomly gives you money that you immediatly burn because it wont be saved cause I haven't yet made a system to record you balence", inline=True)
      await message.channel.send(embed=embedVar)

    elif message.content.startswith('%%coin'):
      await message.channel.send(random.choice(['Heads','tails']))

    elif message.content.startswith('%%messagedev'):
      dmes = message.content.replace("%%messagedev","")
      print(('{}:{}').format([(message.author.name),(dmes)])

    elif message.content.startswith('%%die'):
      dinput = message.content.replace("%%die","")
      await message.channel.send(random.choice(['looks like{} got deadified'.format(dinput), 'looks like{} got 360 no scoped'.format(dinput), 'looks like{} got totally rekt and died'.format(dinput)]))

    elif message.content.startswith('%%wrk'):
      rrnngg = (randint(1,500))
      rand = random.choice(["DANG! he be working you earn {}$ that you immediatly burn because it wont be saved cause I haven't yet made a system to record you balence".format(rrnngg), "DANG! he be not working you earn 0$""])
      await message.channel.send('{}'.format(rand))
      
    else: await message.channel.send('Hey not sure if mispled or whatever but for help use %%help')

client.run(os.getenv('TOKEN'))

标签: pythondiscord.pybots

解决方案


Barmar 通知我,在此之前我在线上缺少一个 ),只是将其发布为答案,以便我可以将其标记为已解决。


推荐阅读