0

I want my bot to forward DM messages from specific ID to a specific channel id in my discord server and to forward messages from the same specific server channel back to the same specific user id in DM.

Issues I have:

  1. When the bot receive a message in DM the bot sends the message to the server channel id then it sends back to DM and I want to stop this loop, if message author id its my bot to not forward it.
  2. If the bot receive 2+ messages at the same time it only forwards the first one and I want it to forward all of them.
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()
Zoe
  • 27,060
  • 21
  • 118
  • 148

1 Answers1

0

Fix of first error is easy. You need to ask for second task only if first task isn't started :

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()

I don't know how to fix second error, but hope this helped.

jcoppens
  • 5,306
  • 6
  • 27
  • 47
Puk3l_YT
  • 17
  • 7