@MilanaLs93

Как получить сообщение с ветки Discord Python?

Необходимо при появлении текста в определенной ветке Дискорда(чужого) отправлять его в Телеграм. Вот что нашла в интернете. Но как их объединить не знаю. Помогите пожалуйста.
1 работает но получает сообщения со всех каналов и веток.
2 не работает
from discord.ext import commands
import discord
 
bot = commands.Bot(command_prefix='.', self_bot=True)
 
@bot.listen() 
async def on_ready():
    print(f'Logged in as {bot.user}')
    
@bot.listen()
async def on_message(message:discord.Message):
    # https://discordpy-self.readthedocs.io/en/latest/api.html?highlight=message#discord.Message
    print(message.author, message.content)
 
bot.run("your user token")
from urllib.parse import quote
import requests
 
auth = "" # the token that you received from BotFather.
chatID = "" # id of the telegram channel to forward the message to.
encoded = quote(f"a & b ?my")
res = requests.post(f"https://api.telegram.org/bot{auth}/sendMessage?chat_id={chatID}&text={encoded}")
print(res.status_code, res.text)
# https://core.telegram.org/bots/api#sendmessage


from discord.ext import commands
import telegram.ext
from telegram.ext import MessageHandler, filters, Updater

telegram_token = 'токен'
telegram_group = 'айди'
discord_discord_channel = 'айди'

@bot.listen()
async def on_ready():
    print(f'Logged in as {bot.user}')
	
class repeater(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_message(self, message):
        homechannel = self.bot.get_channel(discord_channel)
        if message.channel == homechannel:
            Tele_Bot = telegram.Bot(token=telegram_token)
            Tele_Bot.sendMessage(chat_id=telegram_group, text=f'{message.guild.name}:{message.author}:\n{message.content}')
            print('Message Detected from Discord & Sent')
  • Вопрос задан
  • 173 просмотра
Решения вопроса 1
Vindicar
@Vindicar
RTFM!
1 работает но получает сообщения со всех каналов и веток.

Значит, нужно добавить условие, проверяющее message.channel.id на совпадение с заранее заданным.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы