public override Task <RiftMessage> ApplyAsync(RiftMessage message, FormatData data) { if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild)) { TemplateError($"No guild found."); return(Task.FromResult(message)); } return(ReplaceDataAsync(message, guild.Name)); }
public async Task BanAsync(IUser target, string reason, IUser moderator) { (var passed, var sgTarget) = await ValidateAsync(target, reason, moderator); if (!passed) { return; } if (RiftBot.IsAdmin(sgTarget) || await RiftBot.IsModeratorAsync(sgTarget)) { await messageService.SendMessageAsync("mod-friendly-fire", Settings.ChannelId.Commands, new FormatData(moderator.Id)); return; } if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild)) { return; } await DB.ModerationLogs.AddAsync(sgTarget.Id, moderator.Id, "Ban", reason, DateTime.UtcNow, TimeSpan.Zero); (var oldToxicity, var newToxicity) = await GetNewToxicityAsync(sgTarget.Id, ToxicitySource.Ban); await DB.Toxicity.UpdatePercentAsync(sgTarget.Id, newToxicity.Percent); var data = new FormatData(target.Id) { Moderation = new ModerationData { ModeratorId = moderator.Id, TargetId = sgTarget.Id, Reason = reason, } }; await messageService.SendMessageAsync("mod-ban-success", Settings.ChannelId.Chat, data); await guild.AddBanAsync(sgTarget, 1, $"Banned by {moderator}: {reason}"); }
async Task UpdateUsersVoiceUptimeAsync() { if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild)) { return; } var channels = guild.VoiceChannels .Where(x => x.CategoryId == VoiceCategoryId && x.Id != Settings.ChannelId.VoiceSetup) .ToList(); if (!channels.Any()) { return; } var users = new List <ulong>(); foreach (var channel in channels) { if (channel.Users.Count <= 1) { continue; } if (channel.UserLimit == 1) { continue; } foreach (var sgUser in channel.Users) { if (!sgUser.VoiceState.HasValue) { continue; } var voiceState = sgUser.VoiceState.Value; if (voiceState.IsMuted || voiceState.IsDeafened || voiceState.IsSelfMuted || voiceState.IsSelfDeafened) { continue; } users.Add(sgUser.Id); } } if (!users.Any()) { return; } var dbReward = await DB.Rewards.GetAsync(24); var reward = dbReward.ItemReward; foreach (var userId in users) { await rewardService.DeliverToAsync(userId, reward); await DB.Statistics.AddAsync(userId, new StatisticData { VoiceUptime = VoiceRewardsInterval }); } RiftBot.Log.Information($"Gived out voice online rewards for {users.Count.ToString()} user(s)."); }
public async Task SelfTest() { var skipChecks = false; var errors = new List <string>(); var fixedRoles = 0u; var eb = new RiftEmbed().WithTitle("Self-test"); if (!IonicHelper.GetGuild(Settings.App.MainGuildId, out var guild)) { errors.Add($"Guild is null: {nameof(Settings.App.MainGuildId)}"); skipChecks = true; } var channelNames = Settings.ChannelId.GetNames(); foreach (var field in Settings.ChannelId.GetType().GetProperties()) { if (skipChecks) { break; } if (field.GetValue(Settings.ChannelId, null) is ulong value) { if (value == 0ul) { if (channelNames.ContainsKey(field.Name)) { var channelName = channelNames[field.Name]; var guildChannel = guild.Channels.FirstOrDefault( x => x.Name.Equals(channelName, StringComparison.InvariantCultureIgnoreCase)); if (guildChannel is null) { errors.Add($"Channel ID remains undefined: {field.Name} {channelName}"); continue; } Settings.ChannelId.SetValue(field.Name, guildChannel.Id); fixedRoles++; } else { errors.Add($"Channel ID remains undefined: {field.Name}"); continue; } } else if (!IonicHelper.GetTextChannel(Settings.App.MainGuildId, value, out var textChannel) && !IonicHelper.GetVoiceChannel(Settings.App.MainGuildId, value, out var voiceChannel)) { errors.Add($"No channel on server: {field.Name}"); } } } foreach (var field in Settings.Chat.GetType().GetProperties()) { if (skipChecks) { break; } var obj = field.GetValue(Settings.Chat); if (obj is ulong ulongValue) { if (ulongValue == 0ul) { errors.Add($"Chat parameter undefined: {field.Name}"); } } else if (obj is uint uintValue) { if (uintValue == 0u) { errors.Add($"Chat parameter undefined: {field.Name}"); } } } foreach (var field in Settings.Economy.GetType().GetProperties()) { if (skipChecks) { break; } try { var obj = field.GetValue(Settings.Economy); if (obj is ulong ulongValue) { if (ulongValue == 0ul) { errors.Add($"Economy parameter undefined: {field.Name}"); } } else if (obj is uint uintValue) { if (uintValue == 0u) { errors.Add($"Economy parameter undefined: {field.Name}"); } } } catch (TargetInvocationException ex) { errors.Add($"\"{field.Name}\" invokation failed: {ex.Message}"); } catch (Exception ex) { errors.Add($"Economy object exception failed: {ex.Message}"); } } var serverRoles = Context.Guild.Roles.ToList(); var roles = await DB.Roles.GetAllAsync(); foreach (var role in serverRoles) { if (skipChecks) { break; } var matchedRole = roles.FirstOrDefault(x => x.Name.Equals(role.Name)); if (matchedRole is null) { await DB.Roles.AddAsync(role); fixedRoles++; continue; } if (matchedRole.RoleId.Equals(role.Id)) { continue; } matchedRole.RoleId = role.Id; await DB.Roles.UpdateAsync(matchedRole); fixedRoles++; } if (errors.Count == 0) { eb.WithColor(0, 255, 0); eb.WithDescription("OK 👌"); } else { eb.WithColor(255, 0, 0); var errorList = string.Join('\n', errors); if (errorList.Length >= 2048) { errorList = string.Join('\n', errors.Take(10)); eb.WithDescription($"**{errors.Count.ToString()} error(s), showing first 10**\n\n{errorList}"); } else { eb.WithDescription($"**{errors.Count.ToString()} error(s)**\n\n{errorList}"); } } await Context.Channel.SendIonicMessageAsync(new IonicMessage(eb)); if (fixedRoles > 0u) { var embedMsg = new RiftEmbed() .WithColor(255, 255, 0) .WithAuthor("Self-test") .WithDescription($"Fixed {fixedRoles.ToString()} roles."); await Context.Channel.SendIonicMessageAsync(new IonicMessage(embedMsg)); } }