首页 文章

在没有用户消息的情况下向Discord Channel发送消息

提问于
浏览
-1

基本上我正在寻找的是找到 Channels 名称的一些方法,例如公告并向其发送消息 . 我知道无论何时用户通过不和谐发送消息或者如果事件发生在不和谐中,如何发送消息

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

2 回答

  • 3

    您只需要搜索 Channels ,该库的文档就在这里:http://rtd.discord.foxbot.me/

    如果我们只检查某个属性(如果通道是否存活),则没有理由包含所有的抽搐响应对象 . 遍历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

    您只需使用以下代码即可:

    var channelName = client.GetChannel(channelID);
    channelName.SendMessage("test");
    
    • channelName只是Channel的变量名,你可以随心所欲地调用它

    • client是DiscordClient的名称,我看到你称之为客户端,所以我在这里使用它 .

    • ChannelID是您要发送消息的 Channels 的ID . 您可以通过在Discord中启用开发者模式来获取 Channels ID .

    或更短:

    client.GetChannel(channelID).SendMessage("test");
    

相关问题