发送消息到不一致的频道没有来自用户的消息
基本上我正在寻找的是某种方式来找到频道名称,如公告和发送消息。我知道如何每当用户通过不和谐发送消息或发送消息,如果事件发生在不和谐发送消息到不一致的频道没有来自用户的消息
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;
}
}
}
}
你只需要搜索频道,该库的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;
}
}
谢谢。我不会复制代码,除非我的东西不起作用,因为我不想复制整个部分。我试图理解不和谐部分是如何工作的。而且这并不是说我在研究上不好,而是因为我不了解C#足以解码github并理解。那就是为什么我来这里要求一些指导。对不起,如果我懒惰。但是在我想要学习的幕后,但我使用这个程序来学习。 感谢您查找服务器和频道的部分。我确实纳入了这一点 –
我知道这发生在一年前,这实际上不是我想做的事情 - 但是你为Zack增加了额外的一分,他没有赞扬你......所以我会:P –
你可以做到这一点简单地使用此代码:
var channelName = client.GetChannel(channelID);
channelName.SendMessage("test");
- >频道名称仅仅是通道的变量名,你可以把它叫做everythig你想
- >客户端是DiscordClient的名称,我看你称它为客户端,所以我在这里使用它。
- > ChannelID是要发送消息的通道的ID。您可以通过在Discord中启用开发人员模式来获取通道ID。
或更短:
client.GetChannel(channelID).SendMessage("test");
@caesay,老兄,你一直在想,我没有研究了***了这一点,并不能找到它时,它只是被发送到不和谐,没有任何投入。我在我的问题中说过,我不希望它在前面的代码中为我编码。只想要一个人帮我解释一下,这样我就可以学习。我确实遇到了一些问题,现在我正试图获得一行特定代码的帮助。从字面上看,如何从用户输入输出渠道。 –