【问题标题】:python discord bot forward messages from DMpython discord bot 转发来自 DM 的消息
【发布时间】:2022-01-05 17:45:09
【问题描述】:

我希望我的机器人将 DM 消息从特定 ID 转发到我的 discord 服务器中的特定频道 ID,并将来自同一特定服务器频道的消息转发回 DM 中的同一特定用户 ID。

我遇到的问题:

  1. 当机器人在 DM 中收到消息时,机器人会将消息发送到服务器通道 ID,然后它会发送回 DM,如果消息作者 ID 是我的机器人不转发,我想停止此循环。
  2. 如果机器人同时收到 2 条以上的消息,它只会转发第一条消息,我希望它转发所有消息。
import discum
import config

bot = discum.Client(token=config.token, log=False)


@bot.gateway.command
def on_message(resp):
    if resp.event.ready_supplemental:
        try:
            user = bot.gateway.session.user
            print(f"Forwarding bot started. Logged in as {user['username']}#{user['discriminator']}")
            guilds = bot.gateway.session.guilds
            for guild_id, guild in guilds.items():
                bot.gateway.request.lazyGuild(guild_id, {1: [[0, 99]]}, typing=True, threads=False, activities=True, members=[])
        except Exception as e:
            return
    if resp.event.message:
        message = resp.parsed.auto()
        if not message['content'] or not message['content'] != "":
            return
        try:
            if str(message['channel_id']) == str(config.dm_channel_id) and str(message['author']['id']) != str(config.bot_account_user_id):
                bot.sendMessage(str(config.from_dm_channel_id), message['content'].strip())
            elif str(message['channel_id']) == str(config.to_dm_channel_id) and str(message['author']['id']) != str(config.user_to_dm_channel_id):
                dm = bot.createDM([config.user_to_dm_channel_id]).json()["id"]
                bot.sendMessage(dm, message['content'].strip())
        except Exception as e:
            pass


bot.gateway.run()

【问题讨论】:

    标签: python discord discum


    【解决方案1】:

    修复第一个错误很容易。只有当第一个任务没有开始时,您才需要请求第二个任务:

    import discum
    import config
    
    bot = discum.Client(token=config.token, log=False)
    
    
    @bot.gateway.command
    def on_message(resp):
        if resp.event.ready_supplemental:
            try:
                user = bot.gateway.session.user
                print(f"Forwarding bot started. Logged in as {user['username']}#{user['discriminator']}")
                guilds = bot.gateway.session.guilds
                for guild_id, guild in guilds.items():
                    bot.gateway.request.lazyGuild(guild_id, {1: [[0, 99]]}, typing=True, threads=False, activities=True, members=[])
            except Exception as e:
                return
            time.sleep(1)  #to prevent running second session after first
        elif resp.event.message:  #add elif here to prevent running both sessions at once
            message = resp.parsed.auto()
            if not message['content'] or not message['content'] != "":
                return
            try:
                if str(message['channel_id']) == str(config.dm_channel_id) and str(message['author']['id']) != str(config.bot_account_user_id):
                    bot.sendMessage(str(config.from_dm_channel_id), message['content'].strip())
                elif str(message['channel_id']) == str(config.to_dm_channel_id) and str(message['author']['id']) != str(config.user_to_dm_channel_id):
                    dm = bot.createDM([config.user_to_dm_channel_id]).json()["id"]
                    bot.sendMessage(dm, message['content'].strip())
            except Exception as e:
                pass
            time.sleep(1)  #to prevent running first session after second
    
    
    bot.gateway.run()
    

    我不知道如何解决第二个错误,但希望这会有所帮助。

    【讨论】:

    • 我刚刚更改了频道 ID,现在我有了并导入和导出。无论如何感谢您的帮助! :P
    猜你喜欢
    • 2020-12-01
    • 1970-01-01
    • 2021-06-07
    • 2021-03-27
    • 2022-01-14
    • 2020-04-18
    • 2021-10-30
    • 1970-01-01
    • 2021-05-03
    相关资源
    最近更新 更多