首页 > 解决方案 > Discord PY 无法更改发送消息的通道

问题描述

我正在使用 Discord py,但我无法更改发送消息的频道,它是如何工作的:首先我使用命令 !set_channel 设置频道 ID,此 ID 保存在 txt 文档中,之后我使用命令“ !say bla bla”,并且此消息在上一个命令中发送到我设置的频道,在此命令执行之前,我正在切割不需要的垃圾,例如“()”,因为我认为这个垃圾破坏了我的代码。如果我没有剪切代码,这将看起来像“('824312123131822092',)”。当我尝试在设置的频道中发送消息时,我不明白为什么我得到“无”。

  1. 是的,我已将我的不和谐机器人添加到我的服务器,我的机器人仅在我的服务器上。
  2. 是的,我的频道已创建,并且我设置了正确的 ID。
@bot.command()
async def say(msg, *arg):
  file1 = open("channel.txt", "r")
  output = file1.read()
  file1.close()
  replaced_output = output.replace("'", "")
  replaced_output2 = replaced_output.replace("(", "")
  replaced_output3 = replaced_output2.replace(")", "")
  replaced_output4 = replaced_output3.replace(",", "")
  channel = bot.get_channel(replaced_output4)
  author = msg.author.id
  if str(author) != str(owner):
    embed = discord.Embed(description=f"<@{author}> ```You not have rights.```", colour=COLOR)
    await msg.channel.send(embed=embed)
    return
  else:
    if not arg:
     embed = discord.Embed(description="Required argument after !say", colour=COLOR)
     embed.set_author(name="Omega Software",icon_url=msg.guild.icon_url)
     embed.set_footer(text="Created for fun!")
     await msg.channel.send(embed=embed)
    else:
      embed = discord.Embed(description=" ".join(arg), colour=COLOR)
      embed.set_author(name="Omega Software",icon_url=msg.guild.icon_url)
      embed.set_footer(text="Created for fun!")
      print (channel)
      print (replaced_output4)
      if channel:
       await channel.send(embed=embed)
       return

标签: pythondiscorddiscord.pybots

解决方案


推荐阅读