public override bool PreconditionCheckGuild(IGuildCommandContext context, out string message) { GuildBotVarCollection BotVarCollection = BotVarManager.GetGuildBotVarCollection(context.Guild.Id); if (!BotVarCollection.TryGetBotVar(BotVarId, out ulong roleId)) { message = $"The guild config variable `{BotVarId}` has to be set to the correct role id!"; return(false); } bool hasRole = context.GuildUser.Roles.Any(role => { if (role.Id == roleId) { if (roleMention == null) { roleMention = role.Mention; } return(true); } return(false); }); if (!hasRole) { message = $"You do not have required role {(roleMention == null ? Markdown.InlineCodeBlock(roleId) : roleMention)}!"; } else { message = null; } return(hasRole); }
protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { ArgumentContainer argOut = new ArgumentContainer(); if (!ArgumentParsing.TryParseGuildTextChannel(context, context.Arguments.First, out argOut.channel)) { return(Task.FromResult(new ArgumentParseResult(Arguments[0], "Failed to parse to a guild text channel!"))); } if (context.Message.Content.Length > Identifier.Length + context.Arguments.First.Length + 2) { context.Arguments.Index++; string embedText = context.RemoveArgumentsFront(1).Replace("[3`]", "```"); if (JSONContainer.TryParse(embedText, out JSONContainer json, out string errormessage)) { if (EmbedHelper.TryGetMessageFromJSONObject(json, out argOut.embed, out argOut.messageContent, out string error)) { return(Task.FromResult(new ArgumentParseResult(argOut))); } else { return(Task.FromResult(new ArgumentParseResult(Arguments[1], error))); } } else { return(Task.FromResult(new ArgumentParseResult(Arguments[1], $"Unable to parse JSON text to a json data structure! Error: `{errormessage}`"))); } }
private Task executeSyncMode(IDMCommandContext context, IGuildCommandContext guildContext, object parsedArgs) { switch (ExecutionMethod) { case HandledContexts.None: return(context.Channel.SendEmbedAsync("INTERNAL ERROR", true)); case HandledContexts.DMOnly: return(Execute(context, parsedArgs)); case HandledContexts.GuildOnly: return(ExecuteGuild(guildContext, parsedArgs)); case HandledContexts.Both: if (context.IsGuildContext) { return(ExecuteGuild(guildContext, parsedArgs)); } else { return(Execute(context, parsedArgs)); } default: return(Task.CompletedTask); } }
private Task <ArgumentParseResult> parseArguments(IDMCommandContext context, IGuildCommandContext guildContext) { switch (ArgumentParserMethod) { case HandledContexts.None: return(Task.FromResult(ArgumentParseResult.DefaultNoArguments)); case HandledContexts.DMOnly: return(ParseArguments(context)); case HandledContexts.GuildOnly: return(ParseArgumentsGuildAsync(guildContext)); case HandledContexts.Both: if (context.IsGuildContext) { return(ParseArgumentsGuildAsync(guildContext)); } else { return(ParseArguments(context)); } } return(Task.FromResult(new ArgumentParseResult("INTERNAL ERROR"))); }
private Task executeAsyncMode(IDMCommandContext context, IGuildCommandContext guildContext, object parsedArgs) { switch (ExecutionMethod) { case HandledContexts.None: return(context.Channel.SendEmbedAsync("INTERNAL ERROR", true)); case HandledContexts.DMOnly: AsyncCommandContainer.NewAsyncCommand(Execute, context, parsedArgs); break; case HandledContexts.GuildOnly: AsyncCommandContainer.NewAsyncCommand(ExecuteGuild, guildContext, parsedArgs); break; case HandledContexts.Both: if (context.IsGuildContext) { AsyncCommandContainer.NewAsyncCommand(ExecuteGuild, guildContext, parsedArgs); } else { AsyncCommandContainer.NewAsyncCommand(Execute, context, parsedArgs); } break; } return(Task.CompletedTask); }
/// <summary> /// Attempts to parse a guild role /// </summary> /// <param name="context">The guild commandcontext to parse the role with</param> /// <param name="argument">The argument string to parse the role from</param> /// <param name="result">The resulting socketguild user</param> /// <param name="allowMention">Wether mentioning is enabled for parsing role</param> /// <param name="allowId">Wether the ulong id is enabled for parsing role</param> /// <returns>True if parsing was successful</returns> public static bool TryParseRole(IGuildCommandContext context, string argument, out SocketRole result, bool allowMention = true, bool allowId = true, bool allowName = true) { result = null; if (allowMention && argument.StartsWith("<@&") && argument.EndsWith('>') && argument.Length > 3) { if (ulong.TryParse(argument.Substring(3, argument.Length - 4), out ulong roleId)) { result = context.Guild.GetRole(roleId); return(result != null); } } else if (allowId && ulong.TryParse(argument, out ulong roleId)) { result = context.Guild.GetRole(roleId); return(result != null); } else if (allowName) { foreach (SocketRole role in context.Guild.Roles) { if (role.Name == argument) { result = role; return(true); } } } return(false); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { SocketGuildUser User = argObj as SocketGuildUser; EmbedBuilder embed = new EmbedBuilder() { Color = BotCore.EmbedColor, }; SocketGuildUser guildUser = User as SocketGuildUser; if ((guildUser != null) && !string.IsNullOrEmpty(guildUser.Nickname)) { embed.Author = new EmbedAuthorBuilder() { Name = guildUser.Nickname, IconUrl = User.GetAvatarUrl() }; } else { embed.Author = new EmbedAuthorBuilder() { Name = User.Username, IconUrl = User.GetAvatarUrl() }; } embed.ImageUrl = User.GetAvatarUrl(size: 2048); await context.Channel.SendEmbedAsync(embed); }
protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { StoredMessagesService messagesService = StoredMessagesService.GetMessagesService(context.Guild.Id); if (messagesService.QuoteCount == 0) { return(Task.FromResult(new ArgumentParseResult("No quotes saved for this guild!"))); } if (context.Arguments.TotalCount == 0) { SelectedQuote = messagesService.GetRandomQuote(); return(Task.FromResult(ArgumentParseResult.DefaultNoArguments)); } if (!int.TryParse(context.Arguments.First, out int quoteId)) { return(Task.FromResult(new ArgumentParseResult(Arguments[0]))); } if (quoteId < 0) { return(Task.FromResult(new ArgumentParseResult(Arguments[0]))); } if (!messagesService.TryGetQuote(quoteId, out SelectedQuote)) { return(Task.FromResult(new ArgumentParseResult(Arguments[0], $"Couldn't find a quote with Id `{quoteId}`!"))); } return(Task.FromResult(ArgumentParseResult.SuccessfullParse)); }
/// <summary> /// Attempts to parse a guild channel /// </summary> /// <param name="context">The guild commandcontext to parse the channel with</param> /// <param name="argument">The argument string to parse the channel from</param> /// <param name="result">The socketguildchannel result</param> /// <param name="allowMention">Wether mentioning is enabled for parsing role</param> /// <param name="allowThis">Wether pointing to current channel is enabled</param> /// <param name="allowId">Wether the ulong id is enabled for parsing role</param> /// <returns>True, if parsing was successful</returns> public static bool TryParseGuildChannel(IGuildCommandContext context, string argument, out SocketGuildChannel result, bool allowMention = true, bool allowThis = true, bool allowId = true, bool allowName = true) { result = null; if (allowId && ulong.TryParse(argument, out ulong Id)) { result = context.Guild.GetChannel(Id); return(result != null); } if (allowMention && argument.StartsWith("<#") && argument.EndsWith('>') && argument.Length > 3) { if (ulong.TryParse(argument.Substring(2, argument.Length - 3), out ulong Id2)) { result = context.Guild.GetTextChannel(Id2); return(result != null); } } if (allowThis && argument.Equals("this")) { result = context.GuildChannel; return(true); } if (allowName) { foreach (SocketGuildChannel channel in context.Guild.Channels) { if (channel.Name == argument) { result = channel; return(true); } } } return(false); }
private static bool TryParseSocketTextChannelArgumentGuild(IGuildCommandContext context, out SocketTextChannel value) { string argument = context.Arguments.First; value = null; if (argument.Equals("this")) { value = context.GuildChannel; return(value != null); } else if (ulong.TryParse(argument, out ulong Id)) { value = context.Guild.GetTextChannel(Id); return(value != null); } else if (argument.StartsWith("<#") && argument.EndsWith('>') && argument.Length > 3) { if (ulong.TryParse(argument.Substring(2, argument.Length - 3), out ulong Id2)) { value = context.Guild.GetTextChannel(Id2); return(value != null); } } return(false); }
private static bool TryParseSocketVoiceChannelArgumentGuild(IGuildCommandContext context, out SocketVoiceChannel value) { value = null; if (ulong.TryParse(context.Arguments.First, out ulong Id)) { value = context.Guild.GetVoiceChannel(Id); return(value != null); } return(false); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { ArgumentContainer args = argObj as ArgumentContainer; GuildModerationLog guildModerationLog = GuildModerationLog.GetOrCreateGuildModerationLog(context.Guild.Id); UserModerationLog userModerationLog = guildModerationLog.GetOrCreateUserModerationLog(args.UserId); UserModerationEntry moderationEntry = new UserModerationEntry(context.Guild.Id, ModerationType.Note, null, context.GuildUser, args.Note); await userModerationLog.AddModerationEntry(moderationEntry); await context.Channel.SendEmbedAsync($"Added Note `{args.Note}` to modlogs for {(args.TargetUser == null ? Markdown.InlineCodeBlock(args.UserId.ToString()) : args.TargetUser.Mention)}"); }
private Task execute(IDMCommandContext context, IGuildCommandContext guildContext, object parsedArgs) { if (RunInAsyncMode) { return(executeAsyncMode(context, guildContext, parsedArgs)); } else { return(executeSyncMode(context, guildContext, parsedArgs)); } }
protected override async Task ExecuteGuild(IGuildCommandContext context, object parsedArgs) { ArgumentContainer args = parsedArgs as ArgumentContainer; await context.Guild.RemoveBanAsync(args.UserId); GuildModerationLog guildLog = GuildModerationLog.GetOrCreateGuildModerationLog(context.Guild.Id); UserModerationLog userLog = guildLog.GetOrCreateUserModerationLog(args.UserId); var entry = new UserModerationEntry(context.Guild.Id, ModerationType.UnBanned, null, context.GuildUser, args.Reason); await userLog.UnBan(context.Guild, entry); await context.Channel.SendEmbedAsync($"Unbanned `{args.UserId}`"); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { ArgumentContainer args = argObj as ArgumentContainer; if (args.NewNickname == "reset") { args.NewNickname = null; } await args.TargetUser.ModifyAsync(GuildUserProperties => { GuildUserProperties.Nickname = args.NewNickname; }); await context.Channel.SendEmbedAsync($"Successfully renamed {args.TargetUser.Mention}!"); }
public int ViewableCommands(IDMCommandContext context, IGuildCommandContext guildContext) { int count = 0; foreach (Command c in commandsInCollection) { if (c.CanView(context, guildContext, context.UserInfo.IsBotAdmin, out _)) { count++; } } return(count); }
/// <summary> /// Attempts to parse a guild user /// </summary> /// <param name="context">The guild commandcontext to parse the user from</param> /// <param name="argument">The argument string to parse the user from</param> /// <param name="result">The resulting socketguild user</param> /// <param name="allowMention">Wether mentioning is enabled for parsing user</param> /// <param name="allowSelf">Wether pointing to self is allowed</param> /// <param name="allowId">Wether the ulong id is enabled for parsing user</param> /// <returns>True if parsing was successful</returns> public static bool TryParseGuildUser(IGuildCommandContext context, string argument, out SocketGuildUser result, bool allowMention = true, bool allowSelf = true, bool allowId = true, bool allowName = true) { result = null; if (allowSelf && argument.Equals("self")) { result = context.GuildUser; return(true); } else if (allowMention && argument.StartsWith("<@") && argument.EndsWith('>') && argument.Length > 3) { if (argument.StartsWith("<@!") && argument.Length > 4) { if (ulong.TryParse(argument.Substring(3, argument.Length - 4), out ulong userId2)) { result = context.Guild.GetUser(userId2); return(result != null); } } if (ulong.TryParse(argument.Substring(2, argument.Length - 3), out ulong userId)) { result = context.Guild.GetUser(userId); return(result != null); } } else if (allowMention && argument.StartsWith("<@!") && argument.EndsWith('>') && argument.Length > 3) { if (ulong.TryParse(argument.Substring(3, argument.Length - 3), out ulong userId)) { result = context.Guild.GetUser(userId); return(result != null); } } else if (allowId && ulong.TryParse(argument, out ulong userId)) { result = context.Guild.GetUser(userId); return(result != null); } else if (allowName) { foreach (SocketGuildUser user in context.Guild.Users) { if (user.ToString() == argument) { result = user; return(true); } } } return(false); }
protected override async Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { string[] argSections = context.Arguments.First.Split("/", StringSplitOptions.RemoveEmptyEntries); if (argSections.Length < 3) { return(new ArgumentParseResult(Arguments[0])); } if (!(ulong.TryParse(argSections[argSections.Length - 3], out ulong guildId) && ulong.TryParse(argSections[argSections.Length - 2], out ulong channelId) && ulong.TryParse(argSections[argSections.Length - 1], out ulong messageId))) { return(new ArgumentParseResult(Arguments[0])); } SocketGuild guild = BotCore.Client.GetGuild(guildId); if (guild == null) { return(new ArgumentParseResult(Arguments[0])); } if (guild.Id != context.Guild.Id) { return(new ArgumentParseResult(Arguments[0], "Can only add quotes from this guild!")); } SocketTextChannel channel = guild.GetTextChannel(channelId); if (channel == null) { return(new ArgumentParseResult(Arguments[0])); } IMessage message = await channel.GetMessageAsync(messageId); if (message == null) { return(new ArgumentParseResult(Arguments[0])); } NewQuote = new Quote(guildId, message); StoredMessagesService messagesService = StoredMessagesService.GetMessagesService(context.Guild.Id); if (messagesService.HasQuote(message.Id)) { return(new ArgumentParseResult(Arguments[0], "Message already stored as quote!")); } messagesService.AddQuote(NewQuote); return(ArgumentParseResult.SuccessfullParse); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { ArgumentContainer args = argObj as ArgumentContainer; IDMChannel dmchannel = await args.TargetUser.GetOrCreateDMChannelAsync(); await dmchannel.SendMessageAsync($"You have been warned on {context.Guild} for `{args.Warning}`!"); GuildModerationLog guildModerationLog = GuildModerationLog.GetOrCreateGuildModerationLog(context.Guild.Id); UserModerationLog userModerationLog = guildModerationLog.GetOrCreateUserModerationLog(args.TargetUser.Id); UserModerationEntry moderationEntry = new UserModerationEntry(context.Guild.Id, ModerationType.Warning, null, context.GuildUser, args.Warning); await userModerationLog.AddModerationEntry(moderationEntry); await context.Channel.SendEmbedAsync($"Warned {args.TargetUser.Mention} with `{args.Warning}`"); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { SocketGuildUser User = argObj as SocketGuildUser; EmbedBuilder embed = new EmbedBuilder() { Color = BotCore.EmbedColor, Title = "UserInfo" }; if (MinecraftGuildModel.TryGetGuildOfUser(User.Id, out MinecraftGuild minecraftGuild)) { embed.AddField("Minecraft Guild Membership", $"\"{minecraftGuild.Name}\", Rank `{minecraftGuild.GetMemberRank(User.Id)}`"); } embed.AddField("Discriminator", string.Format("{0}#{1}", User.Username, User.Discriminator), true); embed.AddField("Mention", '\\' + User.Mention, true); embed.AddField("Discord Snowflake Id", User.Id, true); SocketGuildUser guildUser = User as SocketGuildUser; if (guildUser != null) { if (!string.IsNullOrEmpty(guildUser.Nickname)) { embed.AddField("Nickname", guildUser.Nickname, true); embed.Author = new EmbedAuthorBuilder() { Name = guildUser.Nickname, IconUrl = User.GetAvatarUrl() }; } if (guildUser.JoinedAt != null) { embed.AddField("Joined " + guildUser.Guild.Name, guildUser.JoinedAt?.ToString("r"), true); } } if (embed.Author == null) { embed.Author = new EmbedAuthorBuilder() { Name = User.Username, IconUrl = User.GetAvatarUrl() }; } embed.AddField("Joined Discord", User.CreatedAt.ToString("r"), true); embed.ImageUrl = User.GetAvatarUrl(); await context.Channel.SendEmbedAsync(embed); }
protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { ArgumentContainer argOut = new ArgumentContainer(); if (!ArgumentParsing.TryParseGuildUser(context, context.Arguments.First, out argOut.TargetUser)) { return(Task.FromResult(new ArgumentParseResult(Arguments[0], $"Could not parse {context.Arguments.First} to a discord guild user!"))); } context.Arguments.Index++; argOut.NewNickname = context.Arguments.First; return(Task.FromResult(new ArgumentParseResult(argOut))); }
protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { ArgumentContainer argOut = new ArgumentContainer(); if (!ArgumentParsing.TryParseGuildUser(context, context.Arguments.First, out argOut.TargetUser)) { return(Task.FromResult(new ArgumentParseResult(Arguments[0]))); } context.Arguments.Index++; argOut.Warning = context.Arguments.First; return(Task.FromResult(new ArgumentParseResult(argOut))); }
protected override Task <ArgumentParseResult> ParseArgumentsGuildAsync(IGuildCommandContext context) { ArgumentContainer args = new ArgumentContainer(); if (!ArgumentParsing.TryParseGuildUser(context, context.Arguments.First, out args.TargetUser, allowSelf: false, allowName: false)) { return(Task.FromResult(new ArgumentParseResult(ARGS[0]))); } context.Arguments.Index++; args.Reason = context.Arguments.First; return(Task.FromResult(new ArgumentParseResult(args))); }
public override bool PreconditionCheckGuild(IGuildCommandContext context, out string message) { message = null; if (context.GuildUser.Id == context.Guild.OwnerId) { return(true); } if (!context.GuildUser.Roles.Any(role => { return(role.Permissions.Administrator == true); })) { message = "You are neither Owner of this Guild or have a role with Admin permission!"; return(false); } return(true); }
protected override Task ExecuteGuild(IGuildCommandContext context) { StoredMessagesService messagesService = StoredMessagesService.GetMessagesService(context.Guild.Id); if (messagesService.Macros.Count == 0) { return(context.Channel.SendEmbedAsync("No macros stored for this guild!")); } return(context.Channel.SendEmbedAsync(new EmbedBuilder() { Title = $"Macros - {messagesService.Macros.Count}", Color = BotCore.EmbedColor, Description = messagesService.Macros.OperationJoinReadonly(", ", macro => { return $"`{macro.Identifier}`"; }) })); }
protected override Task ExecuteGuild(IGuildCommandContext context) { StoredMessagesService messagesService = StoredMessagesService.GetMessagesService(context.Guild.Id); if (Delete) { messagesService.RemoveMacro(MacroIdentifier); return(context.Channel.SendEmbedAsync($"Deleted macro `{MacroIdentifier}`")); } else { messagesService.SetMacro(SelectedMacro); SelectedMacro.Build(out EmbedBuilder embed, out string messagecontent, out _); return(context.Channel.SendMessageAsync(text: messagecontent, embed: embed.Build())); } }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { ArgumentContainer args = argObj as ArgumentContainer; IDMChannel dmchannel = await args.TargetUser.GetOrCreateDMChannelAsync(); await dmchannel.SendMessageAsync($"You have been banned on {context.Guild} for `{args.Reason}`! {(args.BanEnds == DateTimeOffset.MaxValue ? "This is permanent." : $"You will be automatically unbanned in {args.Duration.ToHumanTimeString()} ({args.BanEnds.ToString("u")})")}"); await args.TargetUser.BanAsync(reason : args.Reason); GuildModerationLog guildModerationLog = GuildModerationLog.GetOrCreateGuildModerationLog(context.Guild.Id); UserModerationLog userModerationLog = guildModerationLog.GetOrCreateUserModerationLog(args.TargetUser.Id); UserModerationEntry moderationEntry = new UserModerationEntry(context.Guild.Id, ModerationType.Banned, null, context.GuildUser, args.Reason, $"Duration: `{(args.BanEnds == DateTimeOffset.MaxValue ? "perma" : args.Duration.ToHumanTimeString())}`"); await userModerationLog.AddBan(moderationEntry, args.BanEnds); await context.Channel.SendEmbedAsync($"Banned {args.TargetUser.Mention} for `{args.Reason}`"); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object parsedArgs) { ArgumentContainer args = parsedArgs as ArgumentContainer; UserModerationLog userLog = GuildModerationLog.GetOrCreateUserModerationLog(context.Guild.Id, args.TargetUser.Id, out _); try { UserModerationEntry entry = new UserModerationEntry(context.Guild.Id, ModerationType.UnMuted, null, context.GuildUser, args.Reason); await userLog.RemoveMute(args.TargetUser, entry); } catch (Exception e) { await context.Channel.SendEmbedAsync("Failed to manage roles: " + e.Message, true); } await context.Channel.SendEmbedAsync($"Unmuted {args.TargetUser.Mention} ({args.TargetUser.Id}) with reason `{args.Reason}`"); }
protected override async Task ExecuteGuild(IGuildCommandContext context, object argObj) { ArgumentContainer args = argObj as ArgumentContainer; int removals = 0; IEnumerable <IMessage> messages = await context.Channel.GetMessagesAsync(args.InitalCount, CacheMode.AllowDownload).FlattenAsync(); foreach (IMessage message in messages) { if (IsMessageYoungEnough(message, args) && MessageMatchesSelectFilter(message, args)) { args.RemoveCount--; removals++; await context.Channel.DeleteMessageAsync(message); } } await context.Channel.SendEmbedAsync($"Purged Messages: {removals}"); }
private static bool TryParseSocketGuildArgumentGuild(IGuildCommandContext context, out SocketGuild value) { if (context.Arguments.First.ToLower() == "this") { value = context.Guild; return(value != null); } else if (ulong.TryParse(context.Arguments.First, out ulong guildId)) { value = BotCore.Client.GetGuild(guildId); if (value != null) { return(!value.HasAllMembers || value.Users.Any(user => { return user.Id == context.User.Id; })); } return(false); } value = null; return(false); }