Skip to content

Instantly share code, notes, and snippets.

@DWaffles
Last active August 30, 2021 12:43
Show Gist options
  • Save DWaffles/51d04454ac881751a97d198e2e8cccae to your computer and use it in GitHub Desktop.
Save DWaffles/51d04454ac881751a97d198e2e8cccae to your computer and use it in GitHub Desktop.
// This file is part of the DSharpPlus project.
//
// Copyright (c) 2015 Mike Santiago
// Copyright (c) 2016-2021 DSharpPlus Contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity.Extensions;
namespace DSharpPlus.Test
{
[Group("threads"), Aliases("thread")]
public class TestBotThreadCommands : BaseCommandModule
{
[GroupCommand(), Command("cache")]
public async Task ThreadsAsync(CommandContext ctx)
{
await ctx.TriggerTypingAsync();
var response = $"Total Guild Cached Channels: {ctx.Guild.Channels.Count}\n" +
$"\n**Guild Cached Threads**: {ctx.Guild.Threads.Count}\n" +
$"{String.Join("\n", ctx.Guild.Threads.Values.Select(x => $"{x.Mention} in {x.Parent.Mention}{(x.CurrentMember != null ? ": :white_check_mark: Joined": null)}"))}" +
$"\n\n**Channel Cached Threads**: {ctx.Channel.Threads.Count()}\n" +
$"{String.Join("\n", ctx.Channel.Threads.Select(x => $"{x.Mention} in {x.Parent.Mention}{(x.CurrentMember != null ? ": :white_check_mark: Joined": null)}"))}";
await ctx.RespondAsync(response);
}
[Command("memberslist"), Aliases("ml")]
public async Task ListThreadMembersAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
var threadMembers = await thread.ListJoinedMembersAsync();
await ctx.RespondAsync($"This thread has {threadMembers.Count} members. \n{string.Join("\n", threadMembers.Select(x => $"<@{x.Id}>"))}");
}
[Command("create")]
public async Task CreateThreadFromMessageAsync(CommandContext ctx, string name, string reason = null)
{
await ctx.TriggerTypingAsync();
if (ctx.Message?.ReferencedMessage != null)
{
var thread = await ctx.Channel.CreateThreadAsync(ctx.Message.ReferencedMessage, name, archiveAfter: AutoArchiveDuration.Hour, reason);
await ctx.RespondAsync($"Created {thread.Mention}.");
}
else
{
await ctx.RespondAsync("Quote a message to create a thread from.");
}
}
[Command("create")]
public async Task CreateThreadAsync(CommandContext ctx, string name, bool privateThread = false, string reason = null)
{
await ctx.TriggerTypingAsync();
var thread = await ctx.Channel.CreateThreadAsync(name, archiveAfter: AutoArchiveDuration.Hour, privateThread ? ChannelType.PrivateThread : ChannelType.PublicThread, reason);
await thread.AddThreadMemberAsync(ctx.Member);
await ctx.RespondAsync($"Created {thread.Mention}.");
}
[Command("join")]
public async Task JoinThreadAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
await thread.JoinThreadAsync();
await ctx.RespondAsync($"Joined {thread.Mention}.");
}
[Command("leave")]
public async Task LeaveThreadAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
await thread.LeaveThreadAsync();
await ctx.RespondAsync($"Left {thread.Mention}.");
}
[Command("add")]
public async Task AddThreadMemberAsync(CommandContext ctx, DiscordMember member, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
await thread.AddThreadMemberAsync(member);
await ctx.RespondAsync($"Added {member.Mention} to {thread.Mention}.");
}
[Command("remove")]
public async Task RemoveThreadMemberAsync(CommandContext ctx, DiscordMember member, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
await thread.RemoveThreadMemberAsync(member);
await ctx.RespondAsync($"Removed {member.Mention} to {thread.Mention}.");
}
[Command("sendmessage")] //testing some channel specific methods
public async Task SendMessageAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (!channel.IsThread)
{
await ctx.RespondAsync("This or the given channel is not a thread");
return;
}
var thread = channel as DiscordThreadChannel;
await thread.TriggerTypingAsync();
await Task.Delay(TimeSpan.FromSeconds(1));
await thread.SendMessageAsync("Message Test");
var embed = new DiscordEmbedBuilder()
.WithDescription("Test Embed");
await thread.SendMessageAsync(embed: embed);
//test embed
}
[Command("pins")] //testing some channel specific methods
public async Task GetPinnedMessagesAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
var thread = channel as DiscordThreadChannel; //yeah yeah, might not be a channel but testing it out okay
var pinnedMessages = await thread.GetPinnedMessagesAsync();
var builder = new StringBuilder();
for (var i = 0; i < 5 && i < pinnedMessages.Count; i++)
{
var message = pinnedMessages.ElementAt(i);
builder.AppendLine($"`{String.Join(String.Empty, message.Content.Take(30))}` from {message.Author.Mention}\n[Jumplink]({message.JumpLink})\n");
}
var embed = new DiscordEmbedBuilder()
.WithTitle($"{pinnedMessages.Count} Pinned Messages in {thread.Name}")
.WithDescription(builder.ToString());
await thread.SendMessageAsync(embed: embed);
}
//GetMessagesInternalAsync
[Group("list")]
public class ThreadListCommands : BaseCommandModule
{
[Command("activeall"), Aliases("active", "aa")]
public async Task ListGuildActiveThreadsAsync(CommandContext ctx)
{
await ctx.TriggerTypingAsync();
var queryResult = await ctx.Guild.ListActiveThreadsAsync();
var oThreads = queryResult.Threads.OrderBy(x => x.Parent.Position);
var builder = new StringBuilder();
foreach (var thread in oThreads)
{
builder.AppendLine($"**{thread.Mention}** in {thread.Parent.Mention}\n" +
$"{(thread.CurrentMember != null ? ":white_check_mark: Joined\n" : null)}" +
$"Approximate Members: {thread.MemberCount}");
builder.AppendLine();
}
var embed = new DiscordEmbedBuilder()
.WithTitle($"Total Active Threads ({queryResult.Threads.Count})")
.WithDescription(builder.ToString())
.WithColor(DiscordColor.Yellow);
if (queryResult.HasMore)
embed.WithFooter("Subsequent calls can return more threads.");
await ctx.RespondAsync(embed: embed);
}
[Command("publicarchived"), Aliases("public", "pa")]
public async Task ListChannelPublicArchivedAsync(CommandContext ctx, DiscordChannel channel = null)
{
await ctx.TriggerTypingAsync();
if (channel == null)
channel = ctx.Channel;
if (channel.Type != ChannelType.Text)
{
await ctx.RespondAsync("This or the given channel is a not a text channel.");
return;
}
var queryResult = await channel.ListPublicArchivedThreadsAsync();
var oThreads = queryResult.Threads.OrderBy(x => x.Parent.Position);
var builder = new StringBuilder();
foreach (var thread in oThreads)
{
builder.AppendLine($"**{thread.Mention}**\n" +
$"{(thread.CurrentMember != null ? ":white_check_mark: Joined\n" : null)}" +
$"Approximate Members: {thread.MemberCount}");
builder.AppendLine();
}
var embed = new DiscordEmbedBuilder()
.WithTitle($"Total Channel Public Archived Threads ({queryResult.Threads.Count})")
.WithDescription(builder.ToString())
.WithColor(DiscordColor.Yellow);
if (queryResult.HasMore)
embed.WithFooter("Subsequent calls can return more threads.");
await ctx.RespondAsync(embed: embed);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment