Skip to content

Instantly share code, notes, and snippets.

View Quahu's full-sized avatar

Krzysztof Kowalski Quahu

  • Poland
  • 12:15 (UTC +02:00)
View GitHub Profile
@Quahu
Quahu / purge.cs
Created June 2, 2018 16:52
An example purge command that removes messages from the channel it's used in.
[Command("purge")]
[Alias("clean")]
[Summary("Downloads and removes X messages from the current channel.")]
[RequireUserPermission(ChannelPermission.ManageMessages)]
[RequireBotPermission(ChannelPermission.ManageMessages)]
public async Task PurgeAsync(int amount)
{
// Check if the amount provided by the user is positive.
if (amount <= 0)
{
@Quahu
Quahu / randomimage.cs
Created May 25, 2018 19:37
An example command that sends a random image from a predefined array each time it's used.
// define a static array of paths that we'll choose the image from
// we make it static so it isn't reinstantiated unnecessarily each time we run a command from this module
private static string[] _imagePaths = new[] { "images/hackerman.jpg", "images/computerman.jpg", "images/polarbear.png" };
[Command("image")]
[Alias("picture")]
[Summary("Sends a random image.")]
public async Task ImageAsync()
{
// using a new instance of the Random class we randomize a path with an index from 0 to x - 1, where x is the amount of paths in the array
@Quahu
Quahu / ping.cs
Last active February 12, 2020 01:06
An example ping command. Assumes your modules are using the SocketCommandContext.
[Command("ping")]
[Alias("latency")]
[Summary("Shows the websocket connection's latency and time it takes for me send a message.")]
public async Task PingAsync()
{
// start a new stopwatch to measure the time it takes for us to send a message
var sw = Stopwatch.StartNew();
// send the message and store it for later modification
var msg = await ReplyAsync($"**Websocket latency**: {Context.Client.Latency}ms\n" +
Enumerable.Range(1, 100).Select(x => x % 15 == 0 ? "FizzBuzz" : x % 3 == 0 ? "Fizz" : x % 5 == 0 ? "Buzz" : x.ToString()).ToList().ForEach(Console.WriteLine);
@Quahu
Quahu / humantype.cs
Last active February 12, 2020 01:05
Humanize C# generic types.
public static string Humanize(this Type type, bool includeNamespace = false)
{
// simply return its name
if (!type.IsGenericType)
return includeNamespace ? $"{type.Namespace}.{type.Name}" : type.Name;
// what are you doing
if (type.IsNested && type.DeclaringType.IsGenericType)
throw new NotImplementedException();