基本上我正在寻找的是某种方法来查找频道名称,例如公告并向其发送消息。每当用户通过不和谐发送消息或事件在不和谐中发生时,我知道如何发送消息
e.Server.FindChannels("Channel Name").FirstorDefault;
await channel.sendmessage(string.Format("Message"))
但我希望在 Twitch 上发生事件时发送消息。
我目前的代码是这样的:
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
}
我从中提取代码的文件:
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;
}
}
}
}