A Python
A Python

Reputation: 93

Echo a message from another app in discord

I have a bot running in a separate app, but there is a specific variable holding data that I want to also be echo'd on my discord server. The bot itself is huge, but I can pin the specific method here

import discord
import asyncio
import rpChat

global note

class EchoBot(rpChat.ChatClient):
    def on_PRI(self, character, message):
        super().on_PRI(character, message)
        note = character + ": " + message
        global note
        if message[:1] == "~":
            super().PRI("A GameMaster", note)

to try to send this message to discord, I have the following. This is not put in the class above, but just below it, and is not in a class itself:

client = discord.Client()

async def discordEcho():
    """Background task that sends message sent from chat to an appropriate Discord channel
    when global messege data is updated"""
    await client.wait_until_ready()
    channel = client.get_channel(serverroom}
    while not client.is_closed():
        global note
        await channel.send(channel, note)

The data to grab the channel IDs are found in a json

file = open("credentials.json", "r", encoding="utf-8")
info = json.load(file)
file.close()

token = info["discord"]
serverroom = info["serverroom"]

client.loop.create_task(discordEcho())
client.run(token)

When I try this, I obtain:

await client.send_message(channel, message)
AttributeError: 'Client' object has no attribute 'send_message'

And I am unsure why. I have built a bot for both this chat platform, and for discord, but this is the first time I've ever attempted to bridge messages between the two. I am sure what I have offered is clear as mud, but any help would be appreciated, if possible.

Edit:

Edited changes to code. It is working thanks to comment below.

Upvotes: 0

Views: 295

Answers (1)

Łukasz Kwieciński
Łukasz Kwieciński

Reputation: 15699

client.send_message is an old, outdated method, it has been replaced with Messageable.send

async def discordEcho():
    """Background task that sends message sent from chat to an appropriate Discord channel
    when global messege data is updated"""
    await client.wait_until_ready()
    channel = client.get_channel(serverroom)
    while not client.is_closed():
        global note
        await channel.send(note)

Side note: you first need to wait_until_ready before getting the channel to wait until the cache is done loading, otherwise channel will be None

Upvotes: 1

Related Questions