2017-03-16 99 views
-1

基本上我正在尋找的是某種方式來找到頻道名稱,如公告和發送消息。我知道如何每當用戶通過不和諧發送消息或發送消息,如果事件發生在不和諧發送消息到不一致的頻道沒有來自用戶的消息

e.Server.FindChannels("Channel Name").FirstorDefault; 
await channel.sendmessage(string.Format("Message")) 

但我期待在事件上抽搐發生發送消息。

我當前的代碼是這樣的:

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; 
      } 
     } 
    } 
} 
+0

@caesay,老兄,你一直在想,我沒有研究了***了這一點,並不能找到它時,它只是被髮送到不和諧,沒有任何投入。我在我的問題中說過,我不希望它在前面的代碼中爲我編碼。只想要一個人幫我解釋一下,這樣我就可以學習。我確實遇到了一些問題,現在我正試圖獲得一行特定代碼的幫助。從字面上看,如何從用戶輸入輸出渠道。 –

回答

1

你只需要搜索頻道,該庫的docuementation是這裏:http://rtd.discord.foxbot.me/

如果我們只檢查某個屬性(如果通道存在或不存在),沒有理由包含所有的twitch響應對象。遍歷JSON樹並查找特定的項目非常簡單。

我已經在下面的代碼中完成了該問題,並且添加了一些註釋,希望能夠幫助您學習。將以下代碼粘貼到控制檯應用程序的Program.cs中。

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; 
    } 
} 
+0

謝謝。我不會複製代碼,除非我的東西不起作用,因爲我不想複製整個部分。我試圖理解不和諧部分是如何工作的。而且這並不是說我在研究上不好,而是因爲我不瞭解C#足以解碼github並理解。那就是爲什麼我來這裏要求一些指導。對不起,如果我懶惰。但是在我想要學習的幕後,但我使用這個程序來學習。 感謝您查找服務器和頻道的部分。我確實納入了這一點 –

+0

我知道這發生在一年前,這實際上不是我想做的事情 - 但是你爲Zack增加了額外的一分,他沒有讚揚你......所以我會:P –

0

你可以做到這一點簡單地使用此代碼:

var channelName = client.GetChannel(channelID); 
channelName.SendMessage("test"); 

- >頻道名稱僅僅是通道的變量名,你可以把它叫做everythig你想

- >客戶端是DiscordClient的名稱,我看你稱它爲客戶端,所以我在這裏使用它。

- > ChannelID是要發送消息的通道的ID。您可以通過在Discord中啓用開發人員模式來獲取通道ID。

或更短:

client.GetChannel(channelID).SendMessage("test"); 
相關問題