Search code examples
pythondiscorddiscord.pypython-asynciotelethon

How to call a discord bot`s function from another script. Python


I am trying to make a python script, that takes messages from a telegram channel and send them to the discord server`s channel. I am stuck with discord. Absolutely cant have an idea how to make it works.

I already have this main scripts:

# -*- coding: utf-8 -*-

from telethon.sync import TelegramClient
import config
import validators
from multiprocessing import Pipe
from discord_signal import send_message
import asyncio

api_id = config.API_ID
api_hash = config.API_HASH
phone_number = config.PHONE_NUMBER

BASE_URL = 'https://discord.com/api/v10'
CHANNEL_URL = BASE_URL + '/channels/{channel_id}'

class TelegramWorker:
    def __init__(self):
        self.client = TelegramClient('your_account', api_id, api_hash)
        self.client.connect()
        self.client.start()
        self.dp = self.client.get_entity(config.CHANNEL_T)

    def get_messages(self):
        messages = self.client.get_messages(self.dp, limit=1000000000000)
        a = []
        for item in messages:
            item = item.text
            if item:
                if validators.url(item):
                    a.append(item)
        return a

def main():
    tel = TelegramWorker()
    a = tel.get_messages()
    print(a)

    for item in a:
        send_message(item)

if __name__ == '__main__':
    main()

When I run this, python import second script, that run bot with library "discord.py". After it run it, nothing happens.

Here is the second script:

import discord
from discord.ext import commands
import config
import datetime

intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    now_time = datetime.datetime.now()
    print(f'BOT STARTED. TIME: {now_time}')

async def send_message(msg):
    channel = bot.get_channel(id=config.DISCORD_CHANNEL_ID)
    await channel.send(msg)

bot.run(config.DISCORD_TOKEN)

Can anyone help me?


Solution

  • Note that when you import send_message, the compiler will get stuck on the bot.run(config.DISCORD_TOKEN) line, as this function starts an infinite loop to run your discord bot. That is, your telegram bot will never start.

    You can try running your discord bot in a Thread or insert your function that loads telegram messages in the on_ready event.

    However, if you simply want to send a message in a discord channel, you don't need a bot. Just create a Webhook for the channel and copy the link. To post a message to the webhook you can use requests or the SyncWebook provided by discord.py:

    from discord import SyncWebhook
    
    
    WEBHOOK_URL = ""
    
    
    def main():
        webhook = SyncWebhook.from_url(WEBHOOK_URL)
        tel = TelegramWorker()
        a = tel.get_messages()
        print(a)
    
        for item in a:
            webhook.send(a)
    

    Read more details on how to send a message to a discord webhook in this question.