【问题标题】:How to make bot send message to other servers except the one it's in如何让机器人将消息发送到除它所在的服务器之外的其他服务器
【发布时间】:2019-07-27 07:20:42
【问题描述】:

我在下面得到了这个脚本,它将消息发送到所有具有称为“聊天频道”的频道的服务器,但问题是因为它要发送到每个名为“聊天频道”的频道,服务器本身具有该频道所以机器人发送到你发送的服务器中的频道,我也不想要,有什么办法解决这个问题? (In short: The bot should send message to all servers that have the bot and a channel called 'Chat-channel' but not send to that server the message was sent from even thought it has a channel called 'Chat-channel')

Chnls=[]
@bot.event
async def on_message(message):
    if message.author != bot.user:
        for chan in Chnls:
            ch=get(bot.get_all_channels(),id=chan)
            if ch.name=='Chat-channel' and ch.id in Chnls:
                await ch.send(message.content)

【问题讨论】:

  • 为什么不让用户进入您将发布到的频道,并与您的机器人用户交叉引用以排除这些频道
  • 不明白,我该怎么做?

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

您声明您已经拥有要发布到的服务器列表。在每台服务器上,您可以(根据文档:https://discordpy.readthedocs.io/en/latest/api.html#server)调用.members 以返回该服务器中所有成员的迭代。在伪代码中(即需要修改):

for server in server_list: # since you already have this list
    for member in server.members: # maybe server.members()??
        if member is my_bot:
            message = False
            break # leave the inner for loop, so message doesn't get overwritten next iteration
        else:
            message = True
    if message: # meaning "if our bot is not in this server"
        await bot.say('Some message') # we will message them in this case

【讨论】:

    【解决方案2】:

    再次假设您已经拥有频道 ID 列表,您可以使用此方法来防止您的机器人在频道中发送相同的消息

    chans=[]
    @bot.event
    async def on_message(msg):
        if msg.channel.name == 'chat-chan' and msg.author.bot == False:
            for i in chans:
                if i != msg.channel.id:
                    await bot.send_message(discord.Object(id=i),msg.content)
    
        await bot.process_commands(msg)
    
    

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2018-05-26
      • 1970-01-01
      • 2020-11-19
      • 2021-01-28
      • 2018-06-12
      • 2019-10-17
      • 2018-12-09
      • 1970-01-01
      相关资源
      最近更新 更多