【问题标题】:Shorten code and send to correct channel discord bot python缩短代码并发送到正确的频道 discord bot python
【发布时间】:2021-05-01 22:56:31
【问题描述】:

我一直在为我计划在未来做的一件小事开发一个不和谐机器人,但它并没有按照我想要的方式工作。有没有办法让消息发送到我想要发送的频道?

另外,有什么办法可以缩短我的代码吗?我对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)

【问题讨论】:

    标签: python discord


    【解决方案1】:

    我认为这会以更有条理的方式满足您的需求。我的主要口头禅是 DRY——“不要重复自己”。顺便说一句,如果您有几天 == 小时 == 分钟 == 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)
    

    【讨论】:

      猜你喜欢
      • 2021-07-20
      • 2018-11-24
      • 2021-01-10
      • 2023-03-07
      • 1970-01-01
      • 2018-05-31
      • 2022-01-10
      • 2023-03-27
      • 2020-07-13
      相关资源
      最近更新 更多