public static void QueueMessage( this IDiscordTextChannel channel, IMessageWorker <IDiscordMessage> worker, DiscordEmbed embed = null, string message = "", Stream stream = null, Func <IMessageReference <IDiscordMessage>, IMessageReference <IDiscordMessage> > modifier = null) { if (worker == null) { throw new ArgumentNullException(nameof(worker)); } var @ref = worker.CreateRef(new MessageBucketArgs() { Attachment = stream, Channel = channel, Properties = new MessageArgs(message, embed) }); if (modifier != null) { @ref = modifier(@ref); } worker.Execute(@ref); }
public static async Task <IDiscordMessage> SendToChannelAsync( this DiscordEmbed embed, IDiscordTextChannel channel) { if (channel is IDiscordGuildChannel guildChannel) { var currentGuildUser = await guildChannel.GetSelfAsync(); var permissions = await guildChannel.GetPermissionsAsync(currentGuildUser); if (!permissions.HasFlag(GuildPermission.EmbedLinks)) { if (!string.IsNullOrEmpty(embed.Image?.Url ?? "")) { using WebClient wc = new WebClient(); byte[] image = wc.DownloadData(embed.Image.Url); await using MemoryStream ms = new MemoryStream(image); return(await channel.SendFileAsync(ms, "output.png", embed.ToMessageBuilder().Build())); } else if (!string.IsNullOrEmpty(embed.Thumbnail?.Url ?? "")) { using WebClient wc = new WebClient(); byte[] image = wc.DownloadData(embed.Thumbnail.Url); await using MemoryStream ms = new MemoryStream(image); return(await channel.SendFileAsync(ms, "output.png", embed.ToMessageBuilder().Build())); } else { return(await channel.SendMessageAsync(embed.ToMessageBuilder().Build())); } } } return(await channel.SendMessageAsync("", embed : embed)); }
public static Task QueueAsync( this DiscordEmbed embed, IMessageWorker <IDiscordMessage> worker, IDiscordTextChannel channel, string content = "", Func <IMessageReference <IDiscordMessage>, IMessageReference <IDiscordMessage> > modifier = null) { /*var currentUser = await client.GetSelfAsync(); * var currentGuildUser = await guildChannel.GetUserAsync(currentUser.Id); * var permissions = await guildChannel.GetPermissionsAsync(currentGuildUser); * if(permissions.HasFlag(GuildPermission.EmbedLinks)) * { * if(!string.IsNullOrEmpty(embed.Image?.Url ?? "")) * { * using HttpClient wc = new HttpClient(embed.Image.Url); * await using Stream ms = await wc.GetStreamAsync(); * return channel.QueueMessage(worker, stream: ms, message: embed.ToMessageBuilder().Build()); * } * * if(!string.IsNullOrEmpty(embed.Thumbnail?.Url ?? "")) * { * using HttpClient wc = new HttpClient(embed.Thumbnail.Url); * await using Stream ms = await wc.GetStreamAsync(); * return channel.QueueMessage(worker, stream: ms, message: embed.ToMessageBuilder().Build()); * } * * return channel.QueueMessage(worker, message: embed.ToMessageBuilder().Build()); * }*/ QueueMessage(channel, worker, embed, content, modifier: modifier); return(Task.CompletedTask); }
public async Task StartAsync(IDiscordTextChannel channel) { message = await Root.Build().SendToChannel(channel); (Root as BaseItem).SetMenu(this); (Root as BaseItem).SetParent(null); await Root.SelectAsync(); }
static async Task MainAsync(string[] args) { // Enables library wide logging for all Miki libraries. Consider using this logging for debugging or general information. new LogBuilder().SetLogHeader(x => $"[{DateTime.UtcNow.ToLongTimeString()}]") .AddLogEvent((msg, level) => { if (level >= logLevel) { Console.WriteLine($"{msg}"); } }).Apply(); // A cache client is needed to store ratelimits and entities. // This is an in-memory cache, and will be used as a local storage repository. IExtendedCacheClient cache = new InMemoryCacheClient( new ProtobufSerializer() ); // Discord REST API implementation gets set up. IApiClient api = new DiscordApiClient(Token, cache); // Discord direct gateway implementation. IGateway gateway = new GatewayCluster(new GatewayProperties { ShardCount = 1, ShardId = 0, Token = Token, Compressed = true, WebSocketClientFactory = () => new BasicWebSocketClient() }); // This function adds additional utility, caching systems and more. DiscordClient bot = new DiscordClient(new DiscordClientConfigurations { ApiClient = api, Gateway = gateway, CacheClient = cache }); // Add caching events for the gateway. new BasicCacheStage().Initialize(gateway, cache); // Hook up on the MessageCreate event. This will send every message through this flow. bot.MessageCreate += async(msg) => { if (msg.Content == "ping") { IDiscordTextChannel channel = await msg.GetChannelAsync(); await channel.SendMessageAsync("pong!"); } }; // Start the connection to the gateway. await gateway.StartAsync(); // Wait, else the application will close. await Task.Delay(-1); }
private async Task SendNotADonatorErrorAsync(IDiscordTextChannel channel) { await new EmbedBuilder() { Title = "Sorry!", Description = "... but you haven't donated yet, please support us with a small donation to unlock these commands!", }.AddField("Already donated?", "Make sure to join the Miki Support server and claim your donator status!") .AddField("Where do I donate?", "You can find our patreon at https://patreon.com/mikibot") .ToEmbed().QueueToChannelAsync(channel); }
/// <summary> /// Unlocks the achievement and if not yet added to the database, It'll add it to the database. /// </summary> /// <param name="context">sql context</param> /// <param name="id">user id</param> /// <param name="r">rank set to (optional)</param> /// <returns></returns> public async Task UnlockAsync(IAchievement achievement, IDiscordTextChannel channel, IDiscordUser user, int r = 0) { long userid = user.Id.ToDbLong(); if (await UnlockIsValid(achievement, userid, r)) { await CallAchievementUnlockEventAsync(achievement, user, channel); await Notification.SendAchievementAsync(achievement, channel, user); } }
private async Task OnLevelUpAchievementsAsync( IDiscordUser user, IDiscordTextChannel channel, int level) { var achievements = achievementService.GetAchievement(AchievementIds.LevellingId); int achievementToUnlock = -1; if (level >= 3 && level < 5) { achievementToUnlock = 0; } else if (level >= 5 && level < 10) { achievementToUnlock = 1; } else if (level >= 10 && level < 20) { achievementToUnlock = 2; } else if (level >= 20 && level < 30) { achievementToUnlock = 3; } else if (level >= 30 && level < 50) { achievementToUnlock = 4; } else if (level >= 50 && level < 100) { achievementToUnlock = 5; } else if (level >= 100 && level < 150) { achievementToUnlock = 6; } else if (level >= 150) { achievementToUnlock = 7; } if (achievementToUnlock != -1) { if (app is MikiBotApp botApp) { using var context = await botApp.CreateFromUserChannelAsync(user, channel); await achievementService.UnlockAsync( context, achievements, user.Id, achievementToUnlock); } } }
public static void QueueMessage( this IDiscordTextChannel channel, IContext context, DiscordEmbed embed = null, string message = "", Stream stream = null, Func <IMessageReference <IDiscordMessage>, IMessageReference <IDiscordMessage> > modifier = null) { var worker = context.GetService <IMessageWorker <IDiscordMessage> >(); QueueMessage(channel, worker, embed, message, stream, modifier); }
public static Task QueueAsync( this DiscordEmbed embed, IContext context, IDiscordTextChannel channel, string content = "", Func <IMessageReference <IDiscordMessage>, IMessageReference <IDiscordMessage> > modifier = null) { return(QueueAsync( embed, context.GetService <IMessageWorker <IDiscordMessage> >(), channel, content, modifier)); }
public static async Task SendAchievementAsync(IAchievement d, IDiscordTextChannel channel, IDiscordUser user) { if (channel is IDiscordGuildChannel c) { using (var scope = MikiApp.Instance.Services.CreateScope()) { var context = scope.ServiceProvider.GetService <MikiDbContext>(); var achievementSetting = await Setting.GetAsync(context, channel.Id, DatabaseSettingId.Achievements); if (achievementSetting != 0) { return; } } } await CreateAchievementEmbed(d, user) .QueueToChannelAsync(channel); }
public static async Task SendAchievementAsync(IAchievement d, IDiscordTextChannel channel, IDiscordUser user) { if (channel is IDiscordGuildChannel c) { using (var context = new MikiContext()) { var guild = await c.GetGuildAsync(); int achievementSetting = await Setting.GetAsync(context, (long)guild.Id, DatabaseSettingId.Achievements); if (achievementSetting != 0) { return; } } } await CreateAchievementEmbed(d, user) .QueueToChannelAsync(channel); }
public static async Task SendAchievementAsync( AchievementEntry d, IDiscordTextChannel channel, IDiscordUser user) { using var scope = MikiApp.Instance.Services.CreateScope(); if (channel is IDiscordGuildChannel) { var context = scope.ServiceProvider.GetService <ISettingsService>(); var achievementSetting = await context.GetAsync <AchievementNotificationSetting>( SettingType.Achievements, (long)channel.Id); if (achievementSetting != AchievementNotificationSetting.All) { return; } } var worker = scope.ServiceProvider.GetService <IMessageWorker <IDiscordMessage> >(); await CreateAchievementEmbed(d, user).QueueAsync(worker, channel); }
/// <summary> /// Notification for local user level ups. /// </summary> private async Task OnUserLevelUpAsync(IDiscordUser user, IDiscordTextChannel channel, int level) { using var scope = app.Services.CreateScope(); var services = scope.ServiceProvider; var context = services.GetService <MikiDbContext>(); var settingsService = services.GetService <ISettingsService>(); var localeService = services.GetService <ILocalizationService>(); Locale locale = await localeService.GetLocaleAsync((long)channel.Id) .ConfigureAwait(false); EmbedBuilder embed = new EmbedBuilder() .SetTitle(locale.GetString("miki_accounts_level_up_header")) .SetDescription( locale.GetString( "miki_accounts_level_up_content", $"{user.Username}#{user.Discriminator}", level)) .SetColor(1, 0.7f, 0.2f); if (channel is IDiscordGuildChannel guildChannel) { var guild = await guildChannel.GetGuildAsync().ConfigureAwait(false); var guildId = (long)guild.Id; var rolesObtained = await context.LevelRoles .Where(p => p.GuildId == guildId && p.RequiredLevel == level && p.Automatic) .ToListAsync() .ConfigureAwait(false); var notificationSetting = await settingsService.GetAsync <LevelNotificationsSetting>( SettingType.LevelUps, (long)channel.Id) .ConfigureAwait(false); var setting = notificationSetting .OrElse(LevelNotificationsSetting.RewardsOnly) .Unwrap(); switch (setting) { case LevelNotificationsSetting.None: case LevelNotificationsSetting.RewardsOnly when rolesObtained.Count == 0: return; case LevelNotificationsSetting.All: break; default: throw new InvalidOperationException(); } if (rolesObtained.Count > 0) { var roles = (await guild.GetRolesAsync().ConfigureAwait(false)).ToList(); if (user is IDiscordGuildUser guildUser) { foreach (var role in rolesObtained) { var r = roles.FirstOrDefault(x => x.Id == (ulong)role.RoleId); if (r == null) { continue; } await guildUser.AddRoleAsync(r).ConfigureAwait(false); } } var rewards = string.Join( "\n", rolesObtained.Select(x => $"New Role: **{roles.FirstOrDefault(z => (long)z.Id == x.RoleId)?.Name}**")); embed.AddInlineField("Rewards", rewards); } } await embed.ToEmbed() .QueueAsync(scope.ServiceProvider.GetService <IMessageWorker <IDiscordMessage> >(), channel) .ConfigureAwait(false); }
public static async ValueTask SendAchievementAsync( AchievementObject d, int rank, IDiscordTextChannel channel, IDiscordUser user) => await SendAchievementAsync(d.Entries[rank], channel, user);
public static async ValueTask SendAchievementAsync(AchievementDataContainer d, int rank, IDiscordTextChannel channel, IDiscordUser user) => await SendAchievementAsync(d.Achievements[rank], channel, user);
public async Task CallAchievementUnlockEventAsync(IAchievement achievement, IDiscordUser user, IDiscordTextChannel channel) { if (achievement as AchievementAchievement == null) { return; } long id = user.Id.ToDbLong(); using (var scope = MikiApp.Instance.Services.CreateScope()) { var context = scope.ServiceProvider .GetService <MikiDbContext>(); int achievementCount = await context.Achievements .Where(q => q.UserId == id) .CountAsync(); AchievementPacket p = new AchievementPacket() { discordUser = user, discordChannel = channel, achievement = achievement, count = achievementCount }; await OnAchievementUnlocked?.Invoke(p); } }
public static void QueueMessage( this IDiscordTextChannel channel, IContext context, string message) => QueueMessage(channel, context, null, message);
public async Task CallAchievementUnlockEventAsync(IAchievement achievement, IDiscordUser user, IDiscordTextChannel channel) { DogStatsd.Counter("achievements.gained", 1); if (achievement as AchievementAchievement != null) { return; } long id = user.Id.ToDbLong(); using (var context = new MikiContext()) { int achievementCount = await context.Achievements .Where(q => q.UserId == id) .CountAsync(); AchievementPacket p = new AchievementPacket() { discordUser = user, discordChannel = channel, achievement = achievement, count = achievementCount }; await OnAchievementUnlocked?.Invoke(p); } }