Python 缩短代码并发送到正确的通道不一致

Python 缩短代码并发送到正确的通道不一致,python,discord,Python,Discord,我一直在为我计划在未来做的一件小事开发一个discord机器人,但它没有按我希望的方式工作。有没有办法将消息发送到我希望发送的频道 还有,有没有办法缩短我的代码?我对python真的很陌生,所以不要因为我不知道一些显而易见的事情而欺负我。代码如下: @client.command() @commands.has_any_role("Server Owner") async def announce(ctx, days: int, hrs: int, mins: int):

我一直在为我计划在未来做的一件小事开发一个discord机器人,但它没有按我希望的方式工作。有没有办法将消息发送到我希望发送的频道

还有,有没有办法缩短我的代码?我对python真的很陌生,所以不要因为我不知道一些显而易见的事情而欺负我。代码如下:


@client.command()
@commands.has_any_role("Server Owner")
async def announce(ctx, days: int, hrs: int, mins: int):
  channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
  if days == 0:
    if hrs == 0:
      if mins == 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
      elif mins >= 0:
        await ctx.channel.send(f"Overdrive will make an announcement in {mins} minutes! Make sure your online when it comes!")
      elif mins <= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
      else:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    elif hrs >= 0:
      if mins == 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {hrs} hours! Make sure your online when it comes!")
      elif mins >= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {hrs} hours, and {mins} minutes! Make sure your online when it comes!")
      elif mins <= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
      else:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
    elif hrs <= 0:
      channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
      emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
      await channel.send(embed=emb)
      channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    else:
      channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
      emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
      await channel.send(embed=emb)
      channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
  elif days >= 0:
    if hrs == 0:
      if mins == 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {days} days! Make sure your online when it comes!")
      elif mins >= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {days} days, and {mins} minutes! Make sure your online when it comes!")
      elif mins <= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
      else:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    elif hrs >= 0:
      if mins == 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {days} days, and {hrs} hours! Make sure your online when it comes!")
      elif mins >= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
        await ctx.channel.send(f"Overdrive will make an announcement in {days} days, {hrs} hours, and {mins} minutes! Make sure your online when it comes!")
      elif mins <= 0:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
      else:
        channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
        emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
        await channel.send(embed=emb)
        channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    elif hrs <= 0:
      channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
      emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
      await channel.send(embed=emb)
      channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    else:
      channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
      emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
      await channel.send(embed=emb)
      channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
  elif days <= 0:
    channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
    emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
    await channel.send(embed=emb)
    channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
  else:
    channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
    emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
    await channel.send(embed=emb)
    channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
@announce.error
@commands.has_any_role("Server Owner")
async def announceError(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    await ctx.message.delete()
    channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    await ctx.channel.send(f"Overdrive is about to make an announcement! Make sure you're online when it comes!")
  else:
    channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
    emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
    await channel.send(embed=emb)

@client.command()
@命令。具有任何角色(“服务器所有者”)
异步def公告(ctx,天:整数,小时:整数,分钟:整数):
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
如果天数==0:
如果hrs==0:
如果分钟=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“new channel”)
emb=discord.Embed(color=discord.color.green(),title=“**公告错误**”,description=f“尝试发送公告时发生语法错误”。)
等待通道发送(嵌入=emb)
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
elif分钟>=0:
等待ctx.channel.send(f“Overdrive将在{mins}分钟后发布公告!请确保在它到来时您的联机状态!”)
elif mins=0:
如果分钟=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(f“Overdrive将在{hrs}小时后发布公告!请确保它到来时您的在线状态!”)
elif分钟>=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(f“Overdrive将在{hrs}小时和{mins}分钟内发布公告!请确保它到来时您的联机状态!”)
elif mins=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(f“Overdrive将在{days}天和{mins}分钟后发布公告!请确保在发布时在线!”)
elif mins=0:
如果分钟=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(f“Overdrive将在{days}天和{hrs}小时内发布公告!请确保在发布时在线!”)
elif分钟>=0:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(f“Overdrive将在{days}天、{hrs}小时和{mins}分钟内发布公告!请确保在它到来时在线!”)

elif mins我想这会以一种更有条理的方式满足你的需求。我的主要信条是“不要重复你自己”。顺便说一句,如果你有天==小时==分钟==0,那么你不应该宣布吗


@client.command()
@commands.has_any_role("Server Owner")
async def announce(ctx, days: int, hrs: int, mins: int):
  error = None
  description = None
  announcement = None

  if days < 0 or hrs < 0 or mins < 0:
    error="**Announcement Error**"
    description"A syntax error occured while trying to send announcement."
  elif days == 0 and hrs == 0 and mins == 0:
    announcement="Now is the time for the announcement"
  else:
    announcement = "Overdrive will make an annoucement in"
    if days:
      announcement += f" {days} days"
    if hrs:
      announcement += f" {hrs} hours"
    if mins:
      announcement += f" {mins} mins"
    announcement += "!  Make sure you're online when it comes!"

  if error:
    channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
    emb = discord.Embed(color=discord.Color.green(), title=announcement,description=description)
    await channel.send(embed=emb)
  channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
  if announcement:
    await ctx.channel.send(announcement)

@announce.error
@commands.has_any_role("Server Owner")
async def announceError(ctx, error):
  if isinstance(error, commands.MissingRequiredArgument):
    await ctx.message.delete()
    channel = discord.utils.get(ctx.author.guild.text_channels, name="announcements")
    await ctx.channel.send("Overdrive is about to make an announcement! Make sure you're online when it comes!")
  else:
    channel = discord.utils.get(ctx.author.guild.text_channels, name="new-channel")
    emb = discord.Embed(color=discord.Color.green(), title="**Announcement Error**",description=f"A syntax error occured while trying to send announcement.")
    await channel.send(embed=emb)

@client.command()
@命令。具有任何角色(“服务器所有者”)
异步def公告(ctx,天:整数,小时:整数,分钟:整数):
错误=无
description=无
公告=无
如果天数<0或小时<0或分钟<0:
error=“**公告错误**”
description“尝试发送公告时发生语法错误。”
elif天数=0,小时=0,分钟=0:
announcement=“现在是发布公告的时间”
其他:
announcement=“Overdrive将在中发布公告”
如果天:
公告+=f“{days}天”
如果小时数:
公告+=f“{hrs}小时”
如果是分钟:
公告+=f“{mins}mins”
公告+=“!通知到来时请确保您处于联机状态!”
如果出现错误:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“new channel”)
emb=discord.Embed(color=discord.color.green(),title=announcement,description=description)
等待通道发送(嵌入=emb)
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
如果发布:
等待ctx.channel.send(公告)
@宣布错误
@命令。具有任何角色(“服务器所有者”)
异步错误(ctx,错误):
如果isinstance(错误,commands.MissingRequiredArgument):
等待ctx.message.delete()
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“公告”)
等待ctx.channel.send(“Overdrive即将发布公告!请确保它到来时您处于在线状态!”)
其他:
channel=discord.utils.get(ctx.author.guild.text\u channels,name=“new channel”)
emb=discord.Embed(color=discord.color.green(),title=“**公告错误**”,description=f“尝试发送公告时发生语法错误”。)
等待通道发送(嵌入=emb)