public async Task handleCommand_TitleCommand_StreamTitleChangeEventTriggered() { IServiceScopeFactory scopefactory = Substitute.For <IServiceScopeFactory>(); IEventBus eventbus = new EventBusLocal(); StreamTitleChangeArgs receivedEventsArgs = null; //eventbus.TriggerEvent(EventType.).For CommandService service = new CommandService(scopefactory, eventbus); eventbus.StreamTitleChangeRequested += delegate(object sender, StreamTitleChangeArgs e) { receivedEventsArgs = e; }; ChatCommandInputArgs args = new ChatCommandInputArgs() { Message = "!title neuer spiele titel", Sender = "chromos33", ChannelName = "deathmic", Type = BobDeathmic.Data.Enums.ChatType.Twitch, elevatedPermissions = true }; await service.handleCommand(args, BobDeathmic.Data.Enums.ChatType.Twitch, "deathmic"); Assert.AreNotEqual(null, receivedEventsArgs); Assert.AreEqual("neuer spiele titel", receivedEventsArgs.Title); Assert.AreEqual("deathmic", receivedEventsArgs.StreamName); Assert.AreEqual(StreamProviderTypes.Twitch, receivedEventsArgs.Type); }
public static StreamTitleChangeArgs PrepareStreamTitleChange(string StreamName, string Message) { var arg = new StreamTitleChangeArgs(); arg.StreamName = StreamName; arg.Type = StreamProviderTypes.Twitch; if (Message.StartsWith("!stream")) { var questionRegex = Regex.Match(Message, @"game=\'(.*?)\'"); var GameRegex = Regex.Match(Message, @"game=\'(.*?)\'"); string Game = ""; if (GameRegex.Success) { Game = GameRegex.Value; } var TitleRegex = Regex.Match(Message, @"title=\'(.*?)\'"); string Title = ""; if (TitleRegex.Success) { Title = TitleRegex.Value; } arg.Game = Game; arg.Title = Title; } else { if (Message.StartsWith("!game")) { arg.Game = Message.Replace("!game ", ""); arg.Title = ""; } if (Message.StartsWith("!title")) { arg.Title = Message.Replace("!title ", ""); arg.Game = ""; } } return(arg); }
private async void StreamTitleChangeRequested(object sender, StreamTitleChangeArgs e) { using (var scope = _scopeFactory.CreateScope()) { var _context = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); Stream stream = _context.StreamModels.Where(sm => sm.StreamName.ToLower().Equals(e.StreamName.ToLower())).FirstOrDefault(); if (stream != null && stream.ClientID != null && stream.ClientID != "" && stream.AccessToken != null && stream.AccessToken != "") { TwitchAPI api = new TwitchAPI(); api.Settings.ClientId = stream.ClientID; api.Settings.AccessToken = stream.AccessToken; string message = ""; try { if (e.Game != "" && e.Title != "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, status : e.Title, game : e.Game); if (test.Game == e.Game && test.Status == e.Title) { message = "Stream Updated"; } else { message = "Error while updating"; } } if (e.Game != "" && e.Title == "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, game : e.Game); //Does not actually work as request just returns the entered game irrelevant of actual change (always returns true) if (test.Game == e.Game) { message = "Game Updated"; } else { message = "Twitch game mismatch"; } } if (e.Game == "" && e.Title != "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, status : e.Title); if (test.Status == e.Title) { message = "Title Updated"; } else { message = "Title not Updated"; } } } catch (Exception ex) when(ex is TwitchLib.Api.Core.Exceptions.InvalidCredentialException || ex is TwitchLib.Api.Core.Exceptions.BadScopeException) { api.Settings.AccessToken = await RefreshToken(stream.StreamName); if (api.Settings.AccessToken != "") { if (e.Game != "" && e.Title != "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, status : e.Title, game : e.Game); } if (e.Game != "" && e.Title == "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, game : e.Game); } if (e.Game == "" && e.Title != "") { var test = await api.V5.Channels.UpdateChannelAsync(channelId : stream.UserID, status : e.Title); } } } _eventBus.TriggerEvent(EventType.RelayMessageReceived, new Args.RelayMessageArgs() { SourceChannel = stream.DiscordRelayChannel, StreamType = StreamProviderTypes.Twitch, TargetChannel = stream.StreamName, Message = message }); } else { _eventBus.TriggerEvent(EventType.RelayMessageReceived, new Args.RelayMessageArgs() { SourceChannel = stream.DiscordRelayChannel, StreamType = StreamProviderTypes.Twitch, TargetChannel = stream.StreamName, Message = "Stream can't be updated no Authorization key detected." }); } } return; }