Created
April 26, 2019 18:20
-
-
Save TheEagleByte/0e822f9d57073f6e43ca3ed3e78a6ae0 to your computer and use it in GitHub Desktop.
Discord.NET Ticket Module
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Threading.Tasks; | |
using Discord; | |
using Discord.Commands; | |
namespace DiscordBot.Modules | |
{ | |
[Name("Tickets")] | |
[Summary("Ticket Specific Commands")] | |
public class TicketModule : ModuleBase<SocketCommandContext> | |
{ | |
[Command("new", RunMode = RunMode.Async)] | |
[Summary("Creates a new ticket")] | |
public async Task NewTicket(params string[] input) | |
{ | |
var ticketDescription = string.Join(" ", input); | |
try | |
{ | |
// Create a new channel under the tickets category | |
var category = Context.Guild.CategoryChannels.FirstOrDefault(x => string.Equals(x.Name, "Tickets", StringComparison.InvariantCultureIgnoreCase)); | |
var channel = await Context.Guild.CreateTextChannelAsync($"ticket-{DateTime.UtcNow:MM-dd-HH-mm-ss}", | |
properties => properties.CategoryId = category.Id); | |
// Add Permissions for access | |
var supportRole = Context.Guild.Roles.FirstOrDefault(x => string.Equals(x.Name, "Support Team", StringComparison.InvariantCultureIgnoreCase)); | |
var accessPerms = new OverwritePermissions(viewChannel: PermValue.Allow, | |
readMessageHistory: PermValue.Allow, sendMessages: PermValue.Allow); | |
await channel.AddPermissionOverwriteAsync(Context.Guild.EveryoneRole, new OverwritePermissions(viewChannel: PermValue.Deny)); | |
await channel.AddPermissionOverwriteAsync(supportRole, accessPerms); | |
await channel.AddPermissionOverwriteAsync(Context.User, accessPerms); | |
await channel.AddPermissionOverwriteAsync(Context.Client.CurrentUser, accessPerms); | |
// Post to the channel with the information | |
var embedBuilder = new EmbedBuilder | |
{ | |
Color = Color.Green, | |
Description = $"Thanks for contacting us {Context.User.Mention},\n\nPlease provide all details about your issue and we will be with you as soon as a staff member is available.\n\nAny tickets without further information posted within 15 minutes will be closed.\n\nBest regards,\nSupport Team", | |
Fields = new List<EmbedFieldBuilder> | |
{ | |
new EmbedFieldBuilder() | |
.WithName("Subject") | |
.WithValue(ticketDescription) | |
.WithIsInline(true) | |
}, | |
Timestamp = DateTimeOffset.UtcNow | |
}; | |
await channel.SendMessageAsync(embed: embedBuilder.Build()); | |
// Reply to the user that their ticket was created | |
await ReplyAsync($"{Context.User.Mention} A new ticket has been opened in {channel.Mention}"); | |
} | |
catch (Exception) | |
{ | |
await ReplyAsync("Something went wrong. <@AdminUserId> will fix it..."); | |
} | |
} | |
[Command("rename", RunMode = RunMode.Async)] | |
[Summary("Renames a ticket")] | |
public async Task RenameTicket(params string[] input) | |
{ | |
var newTitle = string.Join("-", input); | |
// Make sure the channel is under the tickets category | |
var channel = Context.Guild.GetChannel(Context.Channel.Id); | |
var category = Context.Guild.CategoryChannels.FirstOrDefault(x => x.Name.ToLower() == "tickets"); | |
if (category.Channels.Any(x => x == channel)) | |
{ | |
// Make sure they have the support team role | |
var user = Context.Guild.GetUser(Context.User.Id); | |
if (user.Roles.Any(x => string.Equals(x.Name, "Support Team", StringComparison.InvariantCultureIgnoreCase))) | |
{ | |
await channel.ModifyAsync(x => x.Name = newTitle); | |
await ReplyAsync($"This ticket has been renamed to {newTitle}"); | |
} | |
else | |
{ | |
await ReplyAsync("Only members of the Support Team can rename tickets."); | |
} | |
} | |
} | |
[Command("close", RunMode = RunMode.Async)] | |
[Summary("Closes a ticket")] | |
public async Task CloseTicket(params string[] input) | |
{ | |
var notes = string.Join(" ", input); | |
// Make sure the channel is under the tickets category | |
var channel = Context.Guild.GetChannel(Context.Channel.Id); | |
var category = Context.Guild.CategoryChannels.FirstOrDefault(x => x.Name.ToLower() == "tickets"); | |
if (category.Channels.Any(x => x == channel)) | |
{ | |
// Close the ticket and post in tickets log | |
// Post to the channel with the information | |
var embedBuilder = new EmbedBuilder | |
{ | |
Color = Color.Green, | |
Description = $"{Context.User.Mention} has closed {channel.Name}", | |
Fields = new List<EmbedFieldBuilder> | |
{ | |
new EmbedFieldBuilder() | |
.WithName("Reason") | |
.WithValue(notes) | |
.WithIsInline(true) | |
}, | |
Timestamp = DateTimeOffset.UtcNow | |
}; | |
// Delete the ticket channel | |
await channel.DeleteAsync(); | |
// Get the channel to log to | |
var logChannel = Context.Guild.TextChannels.FirstOrDefault(x => string.Equals(x.Name, "tickets-log", StringComparison.CurrentCultureIgnoreCase)); | |
// Send to the ticket log channel info about this closing | |
await logChannel?.SendMessageAsync(embed: embedBuilder.Build()); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment