Skip to content

Instantly share code, notes, and snippets.

@nathan130200
Created December 26, 2017 19:48
Show Gist options
  • Save nathan130200/7128efd57179da14bd79d0979f5cbe65 to your computer and use it in GitHub Desktop.
Save nathan130200/7128efd57179da14bd79d0979f5cbe65 to your computer and use it in GitHub Desktop.
Program.cs
using Autofac;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.Interactivity;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
using System.IO;
using System.Threading;
using DSharpPlus.CommandsNext.Attributes;
using System.Reflection;
using DSharpPlus.Entities;
namespace TheCreepers
{
public class Program
{
public static IContainer Container {
get;
private set;
}
static void Main(string[] args)
{
ContainerBuilder builder = new ContainerBuilder();
{
builder.RegisterType<SpreedSheetApi>().SingleInstance();
builder.RegisterType<TheCreepersBot>().SingleInstance();
}
Container = builder.Build();
var api = Container.Resolve<SpreedSheetApi>();
api.Authenticate();
var bot = Container.Resolve<TheCreepersBot>();
bot.StartAsync().Wait();
}
}
public class TheCreepersBot
{
public DiscordClient DiscordClient { get; private set; }
public TheCreepersBot()
{
DiscordClient = new DiscordClient(new DiscordConfiguration
{
Token = "",
TokenType = TokenType.Bot,
UseInternalLogHandler = true,
LogLevel = DSharpPlus.LogLevel.Info,
AutoReconnect = true,
EnableCompression = true,
DateTimeFormat = "dd/MM/yyyy HH:mm:ss.ffffff"
});
var commandsNext = DiscordClient.UseCommandsNext(new CommandsNextConfiguration
{
IgnoreExtraArguments = true,
StringPrefix = "t3c!",
EnableMentionPrefix = true,
EnableDms = false
});
commandsNext.CommandExecuted += async (e) =>
{
LogManager.GetLogger("CommandsNext").Fatal($"Command Executed: {e.Context.Guild.Name}/{e.Context.User.Username}#{e.Context.User.Discriminator} -> {e.Context.Message.Content}");
};
commandsNext.CommandErrored += async (e) =>
{
LogManager.GetLogger("CommandsNext").Fatal(e.Exception, $"Command Errored: {e.Context.Guild.Name}/{e.Context.User.Username}#{e.Context.User.Discriminator} -> {e.Context.Message.Content}");
await e.Context.RespondAsync($":x: {e.Context.User.Mention} \n {e.Exception.ToString()}");
};
DiscordClient.UseInteractivity(new InteractivityConfiguration
{
PaginationBehaviour = TimeoutBehaviour.Delete,
PaginationTimeout = TimeSpan.FromSeconds(40),
Timeout = TimeSpan.FromSeconds(40)
});
commandsNext.RegisterCommands(Assembly.GetEntryAssembly());
}
public async Task StartAsync()
{
await DiscordClient.ConnectAsync();
await Task.Delay(-1);
}
}
public class SpreedSheetApi
{
public UserCredential Credential { get; private set; }
public SheetsService SheetsService { get; private set; }
public SpreedSheetApi()
{
using (var fs = new FileStream(Directory.GetCurrentDirectory() + @"\Credentials.json", FileMode.Open, FileAccess.Read))
{
Credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(fs).Secrets,
new[] { SheetsService.Scope.SpreadsheetsReadonly },
"user",
CancellationToken.None,
new FileDataStore(Directory.GetCurrentDirectory() + @"\FileDataStore", true)
).Result;
}
}
public void Authenticate()
{
SheetsService = new SheetsService(new BaseClientService.Initializer
{
HttpClientInitializer = Credential,
ApplicationName = "DotNet"
});
}
}
[Group("staff")]
public class StaffCommands
{
[Command("check")]
public async Task TestAsync(CommandContext ctx, DiscordMember member = null)
{
await ctx.TriggerTypingAsync();
if (member == null)
member = ctx.Member;
var service = Program.Container.Resolve<SpreedSheetApi>().SheetsService;
SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get("1LaxR6ugriVSMEzSIhbOLLS6WSqIiNlBay0FXyX2HmK8", @"Form Responses 1!D2:D");
if (request != null)
{
ValueRange response = await request.ExecuteAsync();
string raw = "";
List<string> users = new List<string>();
if (response.Values != null)
{
if (response.Values.Count > 0)
{
foreach (var row in response.Values)
{
users.Add(row[0].ToString());
}
}
}
var interactivity = ctx.Client.GetInteractivityModule();
var usersSorted = users.Distinct().OrderBy(s => s);
if(users.Contains(member.Username + "#" + member.Discriminator))
{
if (member.Roles.Contains(ctx.Guild.GetRole(371026873708314635))){
var x = DiscordEmoji.FromName(ctx.Client, ":x:");
var embed = new DiscordEmbedBuilder()
.WithDescription("Para remover o cargo clique em :x:")
.WithColor(new DiscordColor(0, 122, 204))
.AddField("Entrou Em", member.JoinedAt.ToString("dd/MM/yyyy"), true)
.AddField("É Inscrito?", "Sim", true)
.WithFooter("Solicitado por " + ctx.User.Username + "#" + ctx.User.Discriminator, ctx.User.AvatarUrl);
var message = await ctx.RespondAsync(embed: embed);
await message.CreateReactionAsync(x).ConfigureAwait(false);
var xer = await interactivity.WaitForMessageReactionAsync(xe => xe == x, message, ctx.User);
if (xer != null){
await message.DeleteAllReactionsAsync();
await member.RevokeRoleAsync(ctx.Guild.Roles.Where(role => role.Id == 371026873708314635).FirstOrDefault());
await ctx.RespondAsync($":white_check_mark: {ctx.User.Mention} Removido o cargo inscrito com sucesso!");
}
else
{
await ctx.RespondAsync(":x:");
}
}
else
{
var wcm = DiscordEmoji.FromName(ctx.Client, ":white_check_mark:");
var embed = new DiscordEmbedBuilder()
.WithColor(new DiscordColor(0, 122, 204))
.WithDescription("Para adicionar o cargo inscrito clique em :white_check_mark:")
.AddField("Entrou Em", member.JoinedAt.ToString("dd/MM/yyyy"), true)
.AddField("É Inscrito?", "Não", true)
.WithFooter("Solicitado por " + ctx.User.Username + "#" + ctx.User.Discriminator, ctx.User.AvatarUrl);
var message = await ctx.RespondAsync(embed: embed);
await message.CreateReactionAsync(wcm);
var xer = await interactivity.WaitForMessageReactionAsync(xe => xe == wcm, message, ctx.User);
if(xer != null)
{
await message.DeleteAllReactionsAsync();
var role = ctx.Guild.Roles.Where(r => r.Id == 371026873708314635).FirstOrDefault();
await member.GrantRoleAsync(role);
await ctx.RespondAsync($":white_check_mark: {ctx.User.Mention} Cargo {role.Name} atribuido para {member.Username}#{member.Discriminator}");
}
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment