-1

I have been using the twitch and discord-rewrite api

I have been writing a program where a user in discord can type !Notify (twitch-client-id) which will notify them on discord when one of the people they have followed on twitch has started streaming. The problem I have is that I don't know how I could get the bot to say "name has started streaming" in the channel that the person typed "Notify in. I have got to the point where I have done all the logic for finding when someone is streaming, I just don't know how to print it out

I have tried making a function like:

@client.event async def send(msg): await client.send_message(msg)

and then calling it directly and putting the msg in but that doesn't work

def relay_detection(self):
    while True:
        time.sleep(5)
        if self.api_request == True:
            online = self.new_twitch_streams
            offline = self.offline_twitch_streams
            print("Checking")


            if online != []:
                for x in range(len(online)):
                    self.prev_twitch_streams.append(online[x])
                    print(f"{online[x]} has gone live on Twitch!")
            if offline != []:
                for x in range(len(offline)):
                    self.prev_twitch_streams.remove(offline[x])
                    print(f"{offline[x]} has gone offline on Twitch!")
        self.api_request = False

Let me explain. "online" is a list with all the streamers that are currently streaming and "offline" is a list of all the streamers that just went offline. As you can see I have printed "name has gone live on twitch" and "name has gone offline on twitch" when I detect that someone has started streaming or has stopped streaming. So I need to create a function with the discord-rewrite api to do that.

So, to summarise, I need a function where my bot can print out a msg when I call it. I would like to use the discord-rewrite api, this may have been asked by I just cant find it.

M_M
  • 49
  • 1
  • 5

1 Answers1

0

I'm not sure how the twitch API works but if you need a function that will send a message, that is quite simple. It looks like this is in a cog, if not, simply change all self.client to client.

In your code do you have either this:

client = commands.Bot(command_prefix = '!')

or this:

bot = commands.Bot(command_prefix = '!')

In mine, I have the former so I will be using client. First you need to specify where your function should send the message. If you're on the latest version of discord.py (1.2.3 I believe). You should be able to use this at the top of your function. You will need to replace the number with the id of the channel you wish to send it to.

channel = await client.fetch_channel(00000000000000001)

If you're not on the latest version you may be able to use something like this, replacing the numbers with the ID's of the guild (server) and the channel you wish to send it to.

guild = await client.fetch_guild(0000000000000001)
channel = guild.get_channel(0000000000000000002)

Both of these will return a Channel where you can send a message to like this:

await channel.send("Message goes here")

So if you wanted to use that function to send a message to discord I would change it to:

def relay_detection(self):
channel = await self.client.fetch_channel(00000000000000001)
while True:
    time.sleep(5)
    if self.api_request == True:
        online = self.new_twitch_streams
        offline = self.offline_twitch_streams
        print("Checking")


        if online != []:
            for x in range(len(online)):
                self.prev_twitch_streams.append(online[x])
                await channel.send(f"{online[x]} has gone live on Twitch!")
        if offline != []:
            for x in range(len(offline)):
                self.prev_twitch_streams.remove(offline[x])
                await channel.send(f"{offline[x]} has gone offline on Twitch!")
    self.api_request = False

EDIT: If you are unsure of how to get these ID's that can be explained here:

https://support.discordapp.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-

Damien D
  • 46
  • 4