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

扎克·阿奈特

基本上,我正在寻找的是某种方式来查找频道名称(例如公告)并向其发送消息。我知道如何在用户通过不和谐发送消息时或在不和谐中发生事件时发送消息

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

你只需要搜索频道,图书馆的文档在这里: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;
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

向所有频道发送消息— Discord.py

来自分类Dev

discord.js将消息发送到特定频道

来自分类Dev

Discord Py将消息发送到特定频道

来自分类Dev

Discord.js发送消息到特定频道

来自分类Dev

检查用户是否可以在特定频道中发送消息discord.js

来自分类Dev

我的代码有效,但是是否有命令可以在没有用户确认的情况下真正发送 whatsapp 消息?

来自分类Dev

Discord.js搜索特定的频道名称以发送消息到(日志)

来自分类Dev

使用discord.py将消息发送到特定频道

来自分类Dev

在discord.py中,如何在刚创建的频道上发送消息?

来自分类Dev

Discord.py如何使漫游器将消息发送到特定频道

来自分类Dev

无法将消息发送到不和谐频道discord.py

来自分类Dev

尝试使用Discord.js使用Discord机器人将消息发送到特定频道

来自分类Dev

Discord Bot 应删除“用户固定消息到频道”消息

来自分类Dev

如何强制Discord在没有嵌入的情况下发送消息?

来自分类Dev

如何使我的discord.py机器人将我选择的消息发送到我选择的频道?

来自分类Dev

如何将不和谐消息发送到另一个频道?(Discord JS)

来自分类Dev

通过API向YouTube频道发送消息

来自分类Dev

如何向标记的频道发送消息?

来自分类Dev

Discord bot发送生日消息

来自分类Dev

我可以在不加入的情况下向IRC频道发送消息吗?

来自分类Dev

Discord.JS发送消息给被标记的用户

来自分类Dev

如何使Discord Bot删除频道中的所有消息?

来自分类Dev

如何使Discord Bot删除频道中的所有消息?

来自分类Dev

discord.py如何向特定用户发送私人消息

来自分类Dev

如何发送消息到Slack监听频道?

来自分类Dev

使用Django频道向单个用户发送消息

来自分类Dev

grpc 是否可以向所有子频道发送消息?

来自分类Dev

使用Discord API在给定用户ID的情况下发送私人消息

来自分类Dev

发送 webhook 消息以通过 discord py 克隆用户消息

Related 相关文章

  1. 1

    向所有频道发送消息— Discord.py

  2. 2

    discord.js将消息发送到特定频道

  3. 3

    Discord Py将消息发送到特定频道

  4. 4

    Discord.js发送消息到特定频道

  5. 5

    检查用户是否可以在特定频道中发送消息discord.js

  6. 6

    我的代码有效,但是是否有命令可以在没有用户确认的情况下真正发送 whatsapp 消息?

  7. 7

    Discord.js搜索特定的频道名称以发送消息到(日志)

  8. 8

    使用discord.py将消息发送到特定频道

  9. 9

    在discord.py中,如何在刚创建的频道上发送消息?

  10. 10

    Discord.py如何使漫游器将消息发送到特定频道

  11. 11

    无法将消息发送到不和谐频道discord.py

  12. 12

    尝试使用Discord.js使用Discord机器人将消息发送到特定频道

  13. 13

    Discord Bot 应删除“用户固定消息到频道”消息

  14. 14

    如何强制Discord在没有嵌入的情况下发送消息?

  15. 15

    如何使我的discord.py机器人将我选择的消息发送到我选择的频道?

  16. 16

    如何将不和谐消息发送到另一个频道?(Discord JS)

  17. 17

    通过API向YouTube频道发送消息

  18. 18

    如何向标记的频道发送消息?

  19. 19

    Discord bot发送生日消息

  20. 20

    我可以在不加入的情况下向IRC频道发送消息吗?

  21. 21

    Discord.JS发送消息给被标记的用户

  22. 22

    如何使Discord Bot删除频道中的所有消息?

  23. 23

    如何使Discord Bot删除频道中的所有消息?

  24. 24

    discord.py如何向特定用户发送私人消息

  25. 25

    如何发送消息到Slack监听频道?

  26. 26

    使用Django频道向单个用户发送消息

  27. 27

    grpc 是否可以向所有子频道发送消息?

  28. 28

    使用Discord API在给定用户ID的情况下发送私人消息

  29. 29

    发送 webhook 消息以通过 discord py 克隆用户消息

热门标签

归档