Skip to content

Commit

Permalink
Merge pull request #164 from InFTord/master
Browse files Browse the repository at this point in the history
a massive clean up
  • Loading branch information
Naamloos committed Oct 20, 2023
2 parents d2a5175 + 2953099 commit 9f0a369
Show file tree
Hide file tree
Showing 74 changed files with 745 additions and 939 deletions.
2 changes: 1 addition & 1 deletion ModCore/AutoComplete/OwnedTagAutoComplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteC
var interactions = ctx.Client.GetInteractionExtension();
var database = interactions.Services.GetService(typeof(DatabaseContextBuilder)) as DatabaseContextBuilder;

using var db = database.CreateContext();
await using var db = database.CreateContext();
var tags = db.Tags.Where(x => x.GuildId == (long)ctx.Guild.Id && x.ChannelId < 1).ToList();
var channelTags = db.Tags.Where(x => x.ChannelId == (long)ctx.Channel.Id).ToList();

Expand Down
6 changes: 2 additions & 4 deletions ModCore/AutoComplete/ReminderIdAutoComplete.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using ModCore.Database;
using ModCore.Database.DatabaseEntities;
using ModCore.Database.JsonEntities;
using ModCore.Extensions;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -23,7 +21,7 @@ public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteC

DatabaseTimer[] reminders;

using (var db = database.CreateContext())
await using (var db = database.CreateContext())
reminders = db.Timers.Where(xt =>
xt.ActionType == TimerActionType.Reminder &&
xt.UserId == (long)ctx.User.Id).ToArray();
Expand Down
2 changes: 1 addition & 1 deletion ModCore/AutoComplete/TagAutoComplete.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public async Task<IEnumerable<DiscordAutoCompleteChoice>> Provider(AutocompleteC
var interactions = ctx.Client.GetInteractionExtension();
var database = interactions.Services.GetService(typeof(DatabaseContextBuilder)) as DatabaseContextBuilder;

using var db = database.CreateContext();
await using var db = database.CreateContext();
var tags = db.Tags.Where(x => x.GuildId == (long)ctx.Guild.Id && x.ChannelId < 1).ToList();
var channelTags = db.Tags.Where(x => x.ChannelId == (long)ctx.Channel.Id).ToList();

Expand Down
7 changes: 0 additions & 7 deletions ModCore/Commands/Config.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Microsoft.VisualBasic;
using ModCore.Components;
using ModCore.Database;
using ModCore.Database.DatabaseEntities;
using ModCore.Database.JsonEntities;
using ModCore.Extensions;
using ModCore.Modals;
using ModCore.Utils.Extensions;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;

namespace ModCore.Commands
Expand Down
2 changes: 0 additions & 2 deletions ModCore/Commands/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using ModCore.Utils.Extensions;
using System;
using System.Globalization;
using System.Text;
using System.Threading.Tasks;

Expand Down
5 changes: 2 additions & 3 deletions ModCore/Commands/Interactive.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using ModCore.Extensions;
using ModCore.Modals;
Expand Down Expand Up @@ -45,7 +44,7 @@ Task collectorTask(DiscordClient sender, DSharpPlus.EventArgs.ComponentInteracti
{
if(e.Message.Id == giveaway.Id && e.User.Id != ctx.User.Id)
{
if(e.Interaction.Data.CustomId == "join" && !members.Any(x => x.Id == e.User.Id))
if(e.Interaction.Data.CustomId == "join" && members.All(x => x.Id != e.User.Id))
{
members.Add(e.User);
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder()
Expand Down Expand Up @@ -77,7 +76,7 @@ await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWi
return;
}

var winnerindex = new Random().Next(0, members.Count() - 1);
var winnerindex = new Random().Next(0, members.Count - 1);
var winner = members[winnerindex];

var tada = DiscordEmoji.FromUnicode("🎉");
Expand Down
60 changes: 29 additions & 31 deletions ModCore/Commands/Level.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public async Task InfoAsync(InteractionContext ctx, [Option("user", "User to get
var experience = 0;
var level = 0;

using (var db = Database.CreateContext())
await using (var db = Database.CreateContext())
{
if(!ctx.Guild.GetGuildSettings(db).Levels.Enabled)
{
Expand All @@ -46,42 +46,40 @@ public async Task InfoAsync(InteractionContext ctx, [Option("user", "User to get
[SlashCommand("leaderboard", "Shows this server's level leaderboard.")]
public async Task LeaderboardAsync(InteractionContext ctx)
{
using (var db = Database.CreateContext())
await using var db = Database.CreateContext();
if (!ctx.Guild.GetGuildSettings(db).Levels.Enabled)
{
if (!ctx.Guild.GetGuildSettings(db).Levels.Enabled)
{
await ctx.CreateResponseAsync("⛔ Levels are disabled in this server!");
return;
}
await ctx.CreateResponseAsync("⛔ Levels are disabled in this server!");
return;
}

var top10 = db.Levels.Where(x => x.GuildId == (long)ctx.Guild.Id)
.OrderByDescending(x => x.Experience)
.Take(10)
.ToList();
var top10 = db.Levels.Where(x => x.GuildId == (long)ctx.Guild.Id)
.OrderByDescending(x => x.Experience)
.Take(10)
.ToList();

if(top10.Count == 0)
{
await ctx.CreateResponseAsync("⚠️ No level data was found for this server!", true);
return;
}
if(top10.Count == 0)
{
await ctx.CreateResponseAsync("⚠️ No level data was found for this server!", true);
return;
}

var top10string = "";
int index = 1;
foreach (var leveldata in top10)
{
top10string += $"{index}. <@{leveldata.UserId}>: Level " +
$"{Listeners.LevelUp.CalculateLevel(leveldata.Experience)} ({leveldata.Experience} xp)\n";
index++;
}
var top10string = "";
int index = 1;
foreach (var leveldata in top10)
{
top10string += $"{index}. <@{leveldata.UserId}>: Level " +
$"{Listeners.LevelUp.CalculateLevel(leveldata.Experience)} ({leveldata.Experience} xp)\n";
index++;
}

var embed = new DiscordEmbedBuilder()
.WithTitle($"{ctx.Guild.Name} Level Leaderboard")
.WithDescription($"These are the users with the most activity!")
.WithColor(new DiscordColor())
.AddField("Top 10", top10string);
var embed = new DiscordEmbedBuilder()
.WithTitle($"{ctx.Guild.Name} Level Leaderboard")
.WithDescription($"These are the users with the most activity!")
.WithColor(new DiscordColor())
.AddField("Top 10", top10string);

await ctx.CreateResponseAsync(embed, true);
}
await ctx.CreateResponseAsync(embed, true);
}
}
}
10 changes: 2 additions & 8 deletions ModCore/Commands/Main.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using ModCore.Entities;
using ModCore.Extensions;
Expand All @@ -10,14 +8,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using ModCore.Database;
using ModCore.Database.JsonEntities;
using System.Runtime.InteropServices;
using Microsoft.Data.SqlClient;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.IO;
using Microsoft.Extensions.Caching.Memory;
Expand Down Expand Up @@ -128,7 +122,7 @@ private async Task doSnipeAsync(InteractionContext ctx, bool edit)
{
var content = message.Content;
if (content.Length > 500)
content = content.Substring(0, 500) + "...";
content = content[..500] + "...";

var embed = new DiscordEmbedBuilder()
.WithAuthor($"{message.Author.GetDisplayUsername()}" + (edit ? " (Edited)" : ""),
Expand Down Expand Up @@ -164,7 +158,7 @@ private async Task doSnipeAsync(InteractionContext ctx, bool edit)

var customId = ExtensionStatics.GenerateIdString("del", new Dictionary<string, string>()
{
{"u", ctx.User.Id.ToString() + "|" + message.Author.Id.ToString() },
{"u", ctx.User.Id + "|" + message.Author.Id },
});

response.AddComponents(new DiscordButtonComponent(ButtonStyle.Danger, customId, "", emoji: new DiscordComponentEmoji("🗑")));
Expand Down
16 changes: 9 additions & 7 deletions ModCore/Commands/Moderation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class Moderation : ApplicationCommandModule
UserId = (long)member.Id
});

using (var db = this.Database.CreateContext())
await using (var db = this.Database.CreateContext())
{
db.Timers.Add(reminder);
await db.SaveChangesAsync();
Expand Down Expand Up @@ -208,12 +208,14 @@ await ctx.Client.GetInteractionExtension()
[Option("user_4", "Add another user to isolate")] DiscordUser user4 = null,
[Option("user_5", "Add another user to isolate")] DiscordUser user5 = null)
{
List<DiscordUser> users = new List<DiscordUser>();
users.Add(user);
users.Add(user2);
users.Add(user3);
users.Add(user4);
users.Add(user5);
List<DiscordUser> users = new List<DiscordUser>
{
user,
user2,
user3,
user4,
user5
};
users.RemoveAll(x => x == null);

foreach (var cuser in users)
Expand Down
3 changes: 0 additions & 3 deletions ModCore/Commands/Purge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using ModCore.Utils.Extensions;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

Expand Down
26 changes: 11 additions & 15 deletions ModCore/Commands/Reminders.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Enums;
using DSharpPlus.SlashCommands;
Expand Down Expand Up @@ -35,11 +33,11 @@ public class Reminders : ApplicationCommandModule
[Option("in", "In how long the reminder should trigger.")]string timespan,
[Option("about", "What to remind about.")]string about)
{
var (duration, text) = (TimeSpan.FromSeconds(0), "");
var (duration, _) = (TimeSpan.FromSeconds(0), "");

try
{
(duration, text) = Dates.ParseTime(timespan);
(duration, _) = Dates.ParseTime(timespan);
}catch(Exception)
{
await ctx.CreateResponseAsync(
Expand Down Expand Up @@ -79,7 +77,7 @@ public class Reminders : ApplicationCommandModule
ActionType = TimerActionType.Reminder
};
reminder.SetData(new TimerReminderData { ReminderText = about});
using (var db = this.Database.CreateContext())
await using (var db = this.Database.CreateContext())
{
db.Timers.Add(reminder);
await db.SaveChangesAsync();
Expand All @@ -96,7 +94,7 @@ public async Task ListAsync(InteractionContext ctx)
{
DatabaseTimer[] reminders;

using (var db = this.Database.CreateContext())
await using (var db = this.Database.CreateContext())
reminders = db.Timers.Where(xt =>
xt.ActionType == TimerActionType.Reminder &&
xt.UserId == (long)ctx.User.Id).ToArray();
Expand Down Expand Up @@ -190,16 +188,14 @@ public async Task ClearAsync(InteractionContext ctx)
}
else if(confirmed.Accepted)
{
using (var db = this.Database.CreateContext())
{
DatabaseTimer[] timers = db.Timers.Where(xt => xt.ActionType == TimerActionType.Reminder && xt.UserId == (long)ctx.User.Id).ToArray();
await using var db = this.Database.CreateContext();
DatabaseTimer[] timers = db.Timers.Where(xt => xt.ActionType == TimerActionType.Reminder && xt.UserId == (long)ctx.User.Id).ToArray();

var count = timers.Length;
await Timers.UnscheduleTimersAsync(timers);
var count = timers.Length;
await Timers.UnscheduleTimersAsync(timers);

await ctx.EditFollowupAsync(confirmed.FollowupMessage.Id, new DiscordWebhookBuilder()
.WithContent("✅ Alright, cleared " + count + $" timer{(count > 1? "s" : "")}."));
}
await ctx.EditFollowupAsync(confirmed.FollowupMessage.Id, new DiscordWebhookBuilder()
.WithContent("✅ Alright, cleared " + count + $" timer{(count > 1? "s" : "")}."));
}
else
{
Expand Down

0 comments on commit 9f0a369

Please sign in to comment.