Reputation: 259
Basically what I am looking for is some way to find a channel name such as announcements and send a message to it. I know how to send a message whenever a user sends a message through discord or if an event happens in discord
e.Server.FindChannels("Channel Name").FirstorDefault;
await channel.sendmessage(string.Format("Message"))
but I am looking to send a message when an event happens on Twitch.
My current Code is this:
TwitchAPIexample.RootObject json = TwitchAPIexample.BuildConnect();
if (TwitchAPIexample.TwitchLive(json))
{
var channel = client.GetChannel("announcements"); //Where I need to get channel
//Where I need to send message to channel
}
The file where I am pulling the code from:
using System.Net;
using System.IO;
using Newtonsoft.Json;
namespace MyFirstBot.Plugins
{
public class TwitchAPIexample
{
private const string url = "https://api.twitch.tv/kraken/streams/channel";
public bool isTwitchLive = true;
public static RootObject BuildConnect()
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "Get";
request.Timeout = 12000;
request.ContentType = "application/json";
request.Headers.Add("authorization", "Token");
using (System.IO.Stream s = request.GetResponse().GetResponseStream())
{
using (StreamReader sr = new System.IO.StreamReader(s))
{
var jsonResponse = sr.ReadToEnd();
RootObject r = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
return r;
}
}
}
public class Preview
{
public string small { get; set; }
public string medium { get; set; }
public string large { get; set; }
public string template { get; set; }
}
public class Channel
{
public bool mature { get; set; }
public string status { get; set; }
public string broadcaster_language { get; set; }
public string display_name { get; set; }
public string game { get; set; }
public string language { get; set; }
public int _id { get; set; }
public string name { get; set; }
public string created_at { get; set; }
public string updated_at { get; set; }
public bool partner { get; set; }
public string logo { get; set; }
public string video_banner { get; set; }
public string profile_banner { get; set; }
public object profile_banner_background_color { get; set; }
public string url { get; set; }
public int views { get; set; }
public int followers { get; set; }
}
public class Stream
{
public long _id { get; set; }
public string game { get; set; }
public int viewers { get; set; }
public int video_height { get; set; }
public int average_fps { get; set; }
public int delay { get; set; }
public string created_at { get; set; }
public bool is_playlist { get; set; }
public Preview preview { get; set; }
public Channel channel { get; set; }
}
public class RootObject
{
public Stream stream { get; set; }
}
public static bool TwitchLive(RootObject stream)
{
TwitchAPIexample twitch = new TwitchAPIexample();
string test = stream.stream.ToString();
if(test == null)
{
twitch.isTwitchLive = false;
return false;
}
else if(test != null & twitch.isTwitchLive == false)
{
twitch.isTwitchLive = true;
return true;
}
else
{
return false;
}
}
}
}
Upvotes: 0
Views: 12970
Reputation: 17213
You just need to search for the channel, the docuementation for the library is here: http://rtd.discord.foxbot.me/
There's no reason to contain all of the twitch response objects if we're only checking for a certain property (if the channel is alive or not). It's quite easy to traverse a JSON tree and look for a specific item.
I've completed the problem in the code below, and added comments for you to follow in the hopes it can help you learn. Paste the following code into Program.cs
of a console application.
static void Main(string[] args)
{
DiscordClient client = null;
// initialize client etc here.
string twitchStreamId = "";
string twitchApiKey = "";
string discordServerName = "";
string discordChannelName = "";
// find discord channel
var server = client.FindServers(discordServerName).FirstOrDefault();
var channel = server.FindChannels(discordChannelName, ChannelType.Text).FirstOrDefault();
var lastTwitchStatus = CheckTwitchStatusIsOnline(twitchStreamId, twitchApiKey);
// send a message on startup always
SendDiscordChannelMessage(channel, lastTwitchStatus);
while (true)
{
// check the status every 10 seconds after that and if the status has changed we send a message
Thread.Sleep(10000);
var status = CheckTwitchStatusIsOnline(twitchStreamId, twitchApiKey);
// if the status has changed since the last time we checked, send a message
if (status != lastTwitchStatus)
SendDiscordChannelMessage(channel, status);
lastTwitchStatus = status;
}
}
private static void SendDiscordChannelMessage(Channel channel, bool twitchIsOnline)
{
channel.SendMessage(twitchIsOnline ? "Twitch channel is now live!!" : "Twitch channel is now offline :(");
}
private static bool CheckTwitchStatusIsOnline(string streamId, string twitchApiKey)
{
// send a request to twitch and check whether the stream is live.
var url = "https://api.twitch.tv/kraken/streams/" + streamId;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.Timeout = 12000;
request.ContentType = "application/json";
request.Headers.Add("authorization", twitchApiKey);
using (var sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
var jsonObject = JObject.Parse(sr.ReadToEnd());
var jsonStream = jsonObject["stream"];
// twitch channel is online if stream is not null.
var twitchIsOnline = jsonStream.Type != JTokenType.Null;
return twitchIsOnline;
}
}
Upvotes: 9