Skip to content

Commit

Permalink
- discat rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Pannoniae committed Mar 12, 2024
1 parent 9dccb97 commit 3419223
Show file tree
Hide file tree
Showing 20 changed files with 886 additions and 316 deletions.
4 changes: 2 additions & 2 deletions CommandHandlers/ChatHandler.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using DSharpPlus.Entities;
using DisCatSharp.Entities;

namespace EconomyBot.CommandHandlers;

public class ChatHandler {
public static async Task test2(DiscordGuild guild) {
var messages = await guild.GetChannel(916804452193824809).GetMessagesAsync(1000).ToListAsync();
var messages = await guild.GetChannel(916804452193824809).GetMessagesAsync(1000);
foreach (var message in messages.Reverse<DiscordMessage>()) {
await Console.Out.WriteLineAsync($"({message.Timestamp}) {message.Author}:{message.Content}");
}
Expand Down
24 changes: 11 additions & 13 deletions CommandHandlers/ChatModule.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using System.Collections.Immutable;
using DisCatSharp;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Attributes;
using DisCatSharp.Entities;
using DisCatSharp.Enums;
using EconomyBot.CommandHandlers;

namespace EconomyBot;
Expand Down Expand Up @@ -34,19 +36,15 @@ static IEnumerable<string> ChunksUpTo(string str, int maxChunkSize) {
[Command]
[RequirePermissions(Permissions.ManageMessages)]
public async Task purge(CommandContext ctx, int amt) {
var messages = new List<DiscordMessage>();
IReadOnlyList<DiscordMessage> messages = [];
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
// the library is stupid
if (ctx.Message.Reference != null) {
await Console.Out.WriteLineAsync("Purging from given message!");
await foreach (var message in ctx.Channel.GetMessagesBeforeAsync(ctx.Message.Reference.Message.Id, amt)) {
messages.Add(message);
}
messages = await ctx.Channel.GetMessagesBeforeAsync(ctx.Message.Reference.Message.Id, amt);
}
else {
await foreach (var message in ctx.Channel.GetMessagesAsync(amt)) {
messages.Add(message);
}
messages = await ctx.Channel.GetMessagesAsync(amt);
}

if (ctx.Channel.Id != Program.LOG) {
Expand Down Expand Up @@ -82,14 +80,14 @@ public async Task roll(CommandContext ctx, int sides) {

[Command]
public async Task save(CommandContext ctx) {
var names = (await ctx.Guild.GetAllMembersAsync().ToListAsync()).Select(member => member.ToString());
var names = (await ctx.Guild.GetAllMembersAsync()).Select(member => member.ToString());
await File.WriteAllLinesAsync(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/names.txt",
names);
}

[Command("bishop")]
public async Task saveBishop(CommandContext ctx) {
var messages = (await ctx.Channel.GetMessagesAsync(2000).ToListAsync())
var messages = (await ctx.Channel.GetMessagesAsync(2000))
.Where(msg => msg.Author.Id == 540265036141297676)
.Select(msg => msg.Content);
await File.WriteAllLinesAsync(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/bishop.txt",
Expand Down
21 changes: 14 additions & 7 deletions CommandHandlers/ChatModuleSlash.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DisCatSharp.Entities;
using DisCatSharp.ApplicationCommands;
using DisCatSharp.ApplicationCommands.Attributes;
using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Enums;
using EconomyBot.CommandHandlers;

namespace EconomyBot;

public class ChatModuleSlash : ApplicationCommandModule {
public class ChatModuleSlash : ApplicationCommandsModule {

private async Task CreateResponseAsync(InteractionContext ctx, string text) {
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent(text));
}

[SlashCommand("purge", "")]
public async Task purge(InteractionContext ctx, [Option("amt", "Amount")] long amt) {
var messages = await ctx.Channel.GetMessagesAsync((int)amt).ToListAsync();
var messages = await ctx.Channel.GetMessagesAsync((int)amt);
var message = string.Join("\n", messages.Select(m => $"{m.Timestamp} {m.Author}: {m.Content}"));
await ctx.Guild.GetChannel(Program.LOG).SendMessageAsync(message);
await ctx.Channel.DeleteMessagesAsync(messages);
await ctx.CreateResponseAsync($"Deleted {amt} messages!");
await CreateResponseAsync(ctx, $"Deleted {amt} messages!");
}

[SlashCommand("test2", "")]
Expand All @@ -28,13 +35,13 @@ public async Task roll(InteractionContext ctx) {
[SlashCommand("roll", "")]
public async Task roll(InteractionContext ctx, [Option("sides", "Sides")] long sides) {
var num = new Random().Next(1, (int)(sides+1));
await ctx.CreateResponseAsync($"You rolled {num}!");
await CreateResponseAsync(ctx, $"You rolled {num}!");
}

[SlashCommand("squish", "Squish someone!")]
public async Task squish(InteractionContext ctx, DiscordMember member) {
var cat = "https://cdn.discordapp.com/attachments/1101712131222683659/1128320701456195594/image.png";
await ctx.DeferAsync();
await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource);
await ctx.Channel.SendMessageAsync(new DiscordMessageBuilder()
.WithContent($"{member.DisplayName} was squished by a giant kitten.").WithEmbed(
new DiscordEmbedBuilder().WithImageUrl(
Expand Down
12 changes: 6 additions & 6 deletions CommandHandlers/ImagesModule.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Net.Serialization;
using DisCatSharp;
using DisCatSharp.CommandsNext;
using DisCatSharp.CommandsNext.Attributes;
using DisCatSharp.Entities;
using DisCatSharp.Net.Serialization;
using Newtonsoft.Json.Linq;

namespace EconomyBot;
Expand Down Expand Up @@ -64,7 +64,7 @@ public async Task b(CommandContext ctx) {
var url = responseJson["content_urls"]["desktop"]["page"].Value<string>();

await ctx.RespondAsync(new DiscordEmbedBuilder().WithTitle(title).WithThumbnail(img)
.WithColor(DiscordColor.Rose).WithDescription(content).AddField("Link:", url).Build());
.WithColor(DiscordColor.Rose).WithDescription(content).AddField(new DiscordEmbedField("Link:", url)).Build());
}

[Command("xkcd"), Description("Gets a random XKCD.")]
Expand Down
35 changes: 23 additions & 12 deletions CommandHandlers/ImagesModuleSlash.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DisCatSharp;
using DisCatSharp.Entities;
using DisCatSharp.ApplicationCommands;
using DisCatSharp.ApplicationCommands.Attributes;
using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Enums;
using Newtonsoft.Json.Linq;

namespace EconomyBot;

public class ImagesModuleSlash : ApplicationCommandModule {
public class ImagesModuleSlash : ApplicationCommandsModule {


private readonly HttpClient client = new();

private async Task CreateResponseAsync(InteractionContext ctx, string text) {
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent(text));
}


[SlashCommand("img", "Fetch a wholesome image.")]
public async Task img(InteractionContext ctx, [Option("reddit", "Ignore this option.")] string reddit = "no") {
if (reddit == "reddit") {
var imgProvider = new RedditImageProvider();
var yuri = await imgProvider.getImageFromSub("yuri");
if (yuri == "penis") {
await ctx.CreateResponseAsync("The bot is currently cuddling, sorry.^^");
await CreateResponseAsync(ctx, "The bot is currently cuddling, sorry.^^");
return;
}

Expand All @@ -23,7 +34,7 @@ public async Task img(InteractionContext ctx, [Option("reddit", "Ignore this opt
var imgProvider = new BooruImageProvider();
var yuri = await imgProvider.getRandomYuri();
if (yuri == "penis") {
await ctx.CreateResponseAsync("The bot is currently cuddling, sorry.^^");
await CreateResponseAsync(ctx, "The bot is currently cuddling, sorry.^^");
return;
}

Expand All @@ -36,7 +47,7 @@ private async Task sendFancyEmbed(InteractionContext ctx, string url, string tit
await Console.Out.WriteLineAsync($"URL: {url}");
var messageBuilder = new DiscordInteractionResponseBuilder().AddEmbed(new DiscordEmbedBuilder()
.WithColor(DiscordColor.Rose).WithDescription(title).WithImageUrl(url));
await ctx.CreateResponseAsync(messageBuilder);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, messageBuilder);
}

[SlashCommand("randomxkcd", "Gets a random XKCD.")]
Expand All @@ -49,7 +60,7 @@ public async Task xkcd(InteractionContext ctx) {
num = latestComicJson["num"].Value<int>();
}
catch (Exception e) {
await ctx.CreateResponseAsync("Failed to get XKCD.");
await CreateResponseAsync(ctx, "Failed to get XKCD.");
await Console.Out.WriteLineAsync(e.ToString());
return;
}
Expand All @@ -65,14 +76,14 @@ public async Task xkcd(InteractionContext ctx) {
url = randomComicJson["img"].Value<string>();
}
catch (Exception e) {
await ctx.CreateResponseAsync("Failed to get XKCD.");
await CreateResponseAsync(ctx, "Failed to get XKCD.");
await Console.Out.WriteLineAsync(e.ToString());
return;
}


var embed = new DiscordEmbedBuilder().WithTitle(title).WithColor(DiscordColor.Purple).WithImageUrl(url).WithDescription($"XKCD #{randomXKCD}");
await ctx.CreateResponseAsync(embed);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(embed));
}

[SlashCommand("xkcd", "Gets a specific XKCD.")]
Expand All @@ -89,7 +100,7 @@ public async Task xkcd(InteractionContext ctx, [Option("xkcd", "The XKCD to get.
alt = randomComicJson["alt"].Value<string>();
}
catch (Exception e) {
await ctx.CreateResponseAsync("Failed to get XKCD.");
await CreateResponseAsync(ctx, "Failed to get XKCD.");
await Console.Out.WriteLineAsync(e.ToString());
return;
}
Expand All @@ -103,6 +114,6 @@ public async Task xkcd(InteractionContext ctx, [Option("xkcd", "The XKCD to get.
+ Environment.NewLine
+ alt);

await ctx.CreateResponseAsync(embed);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().AddEmbed(embed));
}
}
4 changes: 2 additions & 2 deletions CommandHandlers/MusicCommon.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using DisCatSharp.CommandsNext;
using DisCatSharp.Entities;

namespace EconomyBot;

Expand Down
Loading

0 comments on commit 3419223

Please sign in to comment.