【问题标题】:This sends the message from one side to other side but cant send from other side这会将消息从一侧发送到另一侧,但不能从另一侧发送
【发布时间】:2022-01-17 04:35:14
【问题描述】:

所以在这段代码中我使用了 replits 数据库扩展 Channel_IDS 是频道 ID 列表

from discord.ext import commands
from replit import db
import asyncio

bot = commands.Bot(command_prefix = ">>>")
Channel_IDS = []
db["Channels"]
Channel_IDS = db["Channels"]
@bot.event
async def on_ready():
  print("logged in as "+str(bot.user))

YES = ["Yes","yes","YES"]
NO = ["No","no","NO"]

@bot.event  
async def on_guild_channel_create(channel):
  #channel = discord.utils.get(channel.guild.channels, name='channel name')
  if channel.name == "global-chat":
    id = channel.id
    Channel_IDS.append(id)
    await channel.send("successfully set <#"+str(id)+"> as global chat")
  id = channel.id
  print (Channel_IDS)

@bot.event
async def on_guild_channel_delete(channel):
  if channel.id in Channel_IDS:
    Channel_IDS.remove(channel.id)
  print(Channel_IDS)
@bot.event
async def on_message(message):
  if message.author == bot.user:
    return
    print("bot msg")
  else:
    if message.channel.id in Channel_IDS:
      print("message received"+str(message.content))
      for chann in Channel_IDS:
        if chann == message.channel.id:
          break
        chenl = bot.get_channel(chann)
        
        await chenl.send("**"+str(message.author)+"**  :globe_with_meridians: " + str(message.content))
        print("message sent" +str(message.content))



db["Channels"] = Channel_IDS
bot.run(".g1e5fCvWLnHBHCWoLBLhmEmWFNk")

现在的问题是,如果服务器 1 发送消息,服务器 2 接收它,但如果服务器 2 发送消息,机器人会看到该消息,但不会将其发送到服务器 1。

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    你的逻辑是错误的:

    if chann == message.channel.id:
        break
    

    您需要使用continue 关键字而不是break 只跳过1 个频道,因为break 会停止循环。

    【讨论】:

    • 所以我写在那里继续而不是中断?
    • @user17632561,是的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 2023-03-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多