NadekoBot/src/NadekoBot/Modules/Searches/StreamNotificationCommands.cs

180 lines
7.1 KiB
C#
Raw Normal View History

2016-12-03 20:14:44 +00:00
using Discord.Commands;
2016-12-16 18:43:57 +00:00
using Discord;
2016-12-03 20:14:44 +00:00
using System.Linq;
using System.Threading.Tasks;
using NadekoBot.Services;
using System.Collections.Generic;
using NadekoBot.Services.Database.Models;
using Microsoft.EntityFrameworkCore;
2017-07-17 19:42:36 +00:00
using NadekoBot.Common.Attributes;
2016-12-03 20:14:44 +00:00
using NadekoBot.Extensions;
2017-07-17 19:42:36 +00:00
using NadekoBot.Modules.Searches.Services;
2016-12-03 20:14:44 +00:00
namespace NadekoBot.Modules.Searches
{
public partial class Searches
{
[Group]
2017-07-15 16:34:34 +00:00
public class StreamNotificationCommands : NadekoSubmodule<StreamNotificationService>
2016-12-03 20:14:44 +00:00
{
private readonly DbService _db;
2016-12-03 20:14:44 +00:00
2017-07-15 16:34:34 +00:00
public StreamNotificationCommands(DbService db)
2016-12-03 20:14:44 +00:00
{
2017-05-27 08:19:27 +00:00
_db = db;
2016-12-03 20:14:44 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
2017-08-06 15:34:52 +00:00
public async Task Smashcast([Remainder] string username) =>
await TrackStream((ITextChannel)Context.Channel, username, FollowedStream.FollowedStreamType.Smashcast)
2016-12-03 20:14:44 +00:00
.ConfigureAwait(false);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task Twitch([Remainder] string username) =>
await TrackStream((ITextChannel)Context.Channel, username, FollowedStream.FollowedStreamType.Twitch)
2016-12-03 20:14:44 +00:00
.ConfigureAwait(false);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
2017-08-06 14:07:48 +00:00
public async Task Mixer([Remainder] string username) =>
await TrackStream((ITextChannel)Context.Channel, username, FollowedStream.FollowedStreamType.Mixer)
2016-12-03 20:14:44 +00:00
.ConfigureAwait(false);
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
public async Task ListStreams()
2016-12-03 20:14:44 +00:00
{
IEnumerable<FollowedStream> streams;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-12-03 20:14:44 +00:00
{
streams = uow.GuildConfigs
2016-12-16 18:43:57 +00:00
.For(Context.Guild.Id,
2016-12-03 20:14:44 +00:00
set => set.Include(gc => gc.FollowedStreams))
.FollowedStreams;
}
if (!streams.Any())
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("streams_none").ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
return;
}
2016-12-17 04:09:04 +00:00
var text = string.Join("\n", await Task.WhenAll(streams.Select(async snc =>
2016-12-03 20:14:44 +00:00
{
2016-12-17 04:09:04 +00:00
var ch = await Context.Guild.GetTextChannelAsync(snc.ChannelId);
2017-02-24 16:10:44 +00:00
return string.Format("{0}'s stream on {1} channel. 【{2}】",
Format.Code(snc.Username),
Format.Bold(ch?.Name ?? "deleted-channel"),
Format.Code(snc.Type.ToString()));
2016-12-17 04:09:04 +00:00
})));
2017-02-24 16:10:44 +00:00
await Context.Channel.SendConfirmAsync(GetText("streams_following", streams.Count()) + "\n\n" + text)
.ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
2016-12-16 18:43:57 +00:00
[RequireUserPermission(GuildPermission.ManageMessages)]
public async Task RemoveStream(FollowedStream.FollowedStreamType type, [Remainder] string username)
2016-12-03 20:14:44 +00:00
{
username = username.ToLowerInvariant().Trim();
var fs = new FollowedStream()
{
2016-12-16 18:43:57 +00:00
ChannelId = Context.Channel.Id,
2016-12-03 20:14:44 +00:00
Username = username,
Type = type
};
bool removed;
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-12-03 20:14:44 +00:00
{
2016-12-16 18:43:57 +00:00
var config = uow.GuildConfigs.For(Context.Guild.Id, set => set.Include(gc => gc.FollowedStreams));
2016-12-03 20:14:44 +00:00
removed = config.FollowedStreams.Remove(fs);
if (removed)
await uow.CompleteAsync().ConfigureAwait(false);
}
if (!removed)
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("stream_no").ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
return;
}
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("stream_removed",
Format.Code(username),
type).ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
[NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)]
public async Task CheckStream(FollowedStream.FollowedStreamType platform, [Remainder] string username)
2016-12-03 20:14:44 +00:00
{
var stream = username?.Trim();
if (string.IsNullOrWhiteSpace(stream))
return;
try
{
2017-05-27 08:19:27 +00:00
var streamStatus = (await _service.GetStreamStatus(new FollowedStream
2016-12-03 20:14:44 +00:00
{
Username = stream,
Type = platform,
}));
if (streamStatus.IsLive)
{
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("streamer_online",
username,
streamStatus.Views)
.ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
else
{
2017-02-24 16:10:44 +00:00
await ReplyConfirmLocalized("streamer_offline",
username).ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
}
catch
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("no_channel_found").ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
}
2017-02-24 16:10:44 +00:00
private async Task TrackStream(ITextChannel channel, string username, FollowedStream.FollowedStreamType type)
2016-12-03 20:14:44 +00:00
{
2016-12-09 03:22:23 +00:00
username = username.ToLowerInvariant().Trim();
2016-12-03 20:14:44 +00:00
var fs = new FollowedStream
{
GuildId = channel.Guild.Id,
ChannelId = channel.Id,
Username = username,
Type = type,
};
StreamStatus status;
try
{
2017-05-27 08:19:27 +00:00
status = await _service.GetStreamStatus(fs).ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
catch
{
2017-02-24 16:10:44 +00:00
await ReplyErrorLocalized("stream_not_exist").ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
return;
}
2017-05-27 08:19:27 +00:00
using (var uow = _db.UnitOfWork)
2016-12-03 20:14:44 +00:00
{
uow.GuildConfigs.For(channel.Guild.Id, set => set.Include(gc => gc.FollowedStreams))
.FollowedStreams
.Add(fs);
await uow.CompleteAsync().ConfigureAwait(false);
}
2017-05-27 08:19:27 +00:00
await channel.EmbedAsync(_service.GetEmbed(fs, status, Context.Guild.Id), GetText("stream_tracked")).ConfigureAwait(false);
2016-12-03 20:14:44 +00:00
}
}
}
2016-12-03 20:17:37 +00:00
}