Skip to content

Instantly share code, notes, and snippets.

@nathan130200
Created December 29, 2018 20:19
Show Gist options
  • Save nathan130200/0c673e94e702ca3af2026e73a1197f8d to your computer and use it in GitHub Desktop.
Save nathan130200/0c673e94e702ca3af2026e73a1197f8d to your computer and use it in GitHub Desktop.
using DSharpPlus.CommandsNext;
using DSharpPlus.Interactivity;
using DSharpPlus.Entities;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus;
using Humanizer;
using GameStage.Entities.Config;
using Microsoft.Extensions.DependencyInjection;
namespace GameStage.Entities.Tools
{
public class VoteSkip
{
public static readonly DiscordEmoji WHITE_CHECK_MARK = DiscordEmoji.FromUnicode("\u2705");
public static readonly DiscordEmoji X = DiscordEmoji.FromUnicode("\u274c");
private CommandContext _ctx;
private string _title;
private VoteSkipConfig _vcfg;
public VoteSkip(CommandContext ctx, string title)
{
_ctx = ctx;
_title = title;
_vcfg = _ctx.Services.GetRequiredService<GameStageBot>()
.Config.VoteSkip;
}
public async Task<bool> CanSkipAsyc()
{
if ((_ctx.Member.PermissionsIn(_ctx.Channel) & Permissions.Administrator) != 0 && !_vcfg.BypassAdministrator)
return true;
var xm = await _ctx.RespondAsync($":interrobang: {_ctx.User.Username}#{_ctx.User.Discriminator} Abriu uma votação! **{_title}**");
var poll = await xm.MakePoll(new[] { WHITE_CHECK_MARK, X }, _vcfg.Timeout);
await xm.DeleteSafeAsync();
if (poll == null)
{
await _ctx.RespondAsync($"{_ctx.User.Mention} {X} Tempo limite esgotado!");
return false;
}
var fact1 = poll.Reactions.GetValueOrDefault(WHITE_CHECK_MARK, 0) / 5;
var fact2 = poll.Reactions.GetValueOrDefault(X, 0) / 5;
_ = Task.Run(async () =>
{
var msg = await _ctx.RespondAsync($":bangbang: Votação **{_title}** encerrada!" +
$"\n\t{WHITE_CHECK_MARK}: {poll.Reactions.GetValueOrDefault(WHITE_CHECK_MARK, 0)}" +
$"\n\t{X}: {poll.Reactions.GetValueOrDefault(X, 0)}");
await Task.Delay(10.Seconds());
await msg.DeleteSafeAsync();
});
if (fact1 > fact2)
{
return true;
}
else if (fact1 < fact2)
{
return false;
}
else if(fact1 == fact2)
{
return true;
}
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment