Sanitized everything i could think of so far which which could cause accidental @everyone or @here mentions

This commit is contained in:
Kwoth 2016-10-09 22:40:14 +02:00
parent 0c9fa4b453
commit a9235dad93
8 changed files with 55 additions and 40 deletions

View File

@ -1,6 +1,7 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
using NLog; using NLog;
@ -42,12 +43,12 @@ namespace NadekoBot.Modules.Administration
if (channel == null) //maybe warn the server owner that the channel is missing if (channel == null) //maybe warn the server owner that the channel is missing
return; return;
var msg = conf.ChannelByeMessageText.Replace("%user%", "**" + user.Username + "**"); var msg = conf.ChannelByeMessageText.Replace("%user%", user.Username).Replace("%server%", user.Guild.Name);
if (string.IsNullOrWhiteSpace(msg)) if (string.IsNullOrWhiteSpace(msg))
return; return;
try try
{ {
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false);
if (conf.AutoDeleteByeMessages) if (conf.AutoDeleteByeMessages)
{ {
var t = Task.Run(async () => var t = Task.Run(async () =>
@ -82,7 +83,7 @@ namespace NadekoBot.Modules.Administration
{ {
try try
{ {
var toDelete = await channel.SendMessageAsync(msg).ConfigureAwait(false); var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false);
if (conf.AutoDeleteGreetMessages) if (conf.AutoDeleteGreetMessages)
{ {
var t = Task.Run(async () => var t = Task.Run(async () =>
@ -172,7 +173,7 @@ namespace NadekoBot.Modules.Administration
conf = uow.GuildConfigs.For(channel.Guild.Id); conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text)) if (!string.IsNullOrWhiteSpace(text))
{ {
conf.ChannelGreetMessageText = text; conf.ChannelGreetMessageText = text.SanitizeMentions();
uow.GuildConfigs.Update(conf); uow.GuildConfigs.Update(conf);
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
@ -180,7 +181,7 @@ namespace NadekoBot.Modules.Administration
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
{ {
await channel.SendMessageAsync("`Current greet message:` " + conf.ChannelGreetMessageText); await channel.SendMessageAsync("`Current greet message:` " + conf.ChannelGreetMessageText.SanitizeMentions());
return; return;
} }
await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false); await channel.SendMessageAsync("New greet message set.").ConfigureAwait(false);
@ -275,7 +276,7 @@ namespace NadekoBot.Modules.Administration
conf = uow.GuildConfigs.For(channel.Guild.Id); conf = uow.GuildConfigs.For(channel.Guild.Id);
if (!string.IsNullOrWhiteSpace(text)) if (!string.IsNullOrWhiteSpace(text))
{ {
conf.ChannelByeMessageText = text; conf.ChannelByeMessageText = text.SanitizeMentions();
uow.GuildConfigs.Update(conf); uow.GuildConfigs.Update(conf);
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
@ -283,7 +284,7 @@ namespace NadekoBot.Modules.Administration
if (string.IsNullOrWhiteSpace(text)) if (string.IsNullOrWhiteSpace(text))
{ {
await channel.SendMessageAsync("`Current bye message:` " + conf.ChannelGreetMessageText); await channel.SendMessageAsync("`Current bye message:` " + conf.ChannelGreetMessageText.SanitizeMentions());
return; return;
} }
await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false); await channel.SendMessageAsync("New bye message set.").ConfigureAwait(false);

View File

@ -56,6 +56,8 @@ namespace NadekoBot.Modules.Games
IsActive = true; IsActive = true;
CurrentSentence = GetRandomSentence(); CurrentSentence = GetRandomSentence();
var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f); var i = (int)(CurrentSentence.Length / WORD_VALUE * 1.7f);
try
{
await channel.SendMessageAsync($@":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false); await channel.SendMessageAsync($@":clock2: Next contest will last for {i} seconds. Type the bolded text as fast as you can.").ConfigureAwait(false);
@ -70,7 +72,7 @@ namespace NadekoBot.Modules.Games
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
await msg.ModifyAsync(m => m.Content = $":book:**{CurrentSentence.Replace(" ", " \x200B")}**:book:").ConfigureAwait(false); await msg.ModifyAsync(m => m.Content = $"**{Format.Sanitize(CurrentSentence.Replace(" ", " \x200B")).SanitizeMentions()}**:book:").ConfigureAwait(false);
sw.Start(); sw.Start();
HandleAnswers(); HandleAnswers();
@ -82,14 +84,19 @@ namespace NadekoBot.Modules.Games
return; return;
} }
}
catch { }
finally
{
await Stop().ConfigureAwait(false); await Stop().ConfigureAwait(false);
} }
}
public string GetRandomSentence() public string GetRandomSentence()
{ {
using (var uow = DbHandler.UnitOfWork()) using (var uow = DbHandler.UnitOfWork())
{ {
return uow.TypingArticles.GetRandom()?.Text ?? "No typing articles found. Use `>typeadd` command to add a new article for typing."; return uow.TypingArticles.GetRandom()?.Text ?? $"No typing articles found. Use `{NadekoBot.ModulePrefixes[typeof(Games).Name]}typeadd` command to add a new article for typing.";
} }
} }
@ -194,7 +201,7 @@ namespace NadekoBot.Modules.Games
uow.TypingArticles.Add(new Services.Database.Models.TypingArticle uow.TypingArticles.Add(new Services.Database.Models.TypingArticle
{ {
Author = imsg.Author.Username, Author = imsg.Author.Username,
Text = text Text = text.SanitizeMentions(),
}); });
await uow.CompleteAsync().ConfigureAwait(false); await uow.CompleteAsync().ConfigureAwait(false);
} }

View File

@ -1,4 +1,5 @@
using NadekoBot.Services; using NadekoBot.Extensions;
using NadekoBot.Services;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -34,7 +35,7 @@ namespace NadekoBot.Modules.Games.Trivia
foreach (var item in arr) foreach (var item in arr)
{ {
var tq = new TriviaQuestion(item["Question"].ToString(), item["Answer"].ToString(), item["Category"]?.ToString()); var tq = new TriviaQuestion(item["Question"].ToString().SanitizeMentions(), item["Answer"].ToString().SanitizeMentions(), item["Category"]?.ToString());
pool.Add(tq); pool.Add(tq);
} }
var r = new NadekoRandom(); var r = new NadekoRandom();

View File

@ -77,7 +77,7 @@ namespace NadekoBot.Modules.Utility
toReturn += $@"`Id:` **{user.Id}** toReturn += $@"`Id:` **{user.Id}**
`Current Game:` **{(user.Game?.Name == null ? "-" : user.Game.Name)}** `Current Game:` **{(user.Game?.Name == null ? "-" : user.Game.Name)}**
`Joined At:` **{user.JoinedAt}** `Joined At:` **{user.JoinedAt}**
`Roles:` **({user.Roles.Count()}) - {string.Join(", ", user.Roles.Select(r => r.Name))}** `Roles:` **({user.Roles.Count()}) - {string.Join(", ", user.Roles.Select(r => r.Name)).SanitizeMentions()}**
`AvatarUrl:` **{user.AvatarUrl}**"; `AvatarUrl:` **{user.AvatarUrl}**";
await msg.Reply(toReturn).ConfigureAwait(false); await msg.Reply(toReturn).ConfigureAwait(false);
} }

View File

@ -1,6 +1,7 @@
using Discord; using Discord;
using Discord.Commands; using Discord.Commands;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
@ -34,7 +35,7 @@ namespace NadekoBot.Modules.Utility
if (quote == null) if (quote == null)
return; return;
await channel.SendMessageAsync("📣 " + quote.Text); await channel.SendMessageAsync("📣 " + quote.Text.SanitizeMentions());
} }
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]

View File

@ -2,6 +2,7 @@
using Discord.Commands; using Discord.Commands;
using Discord.WebSocket; using Discord.WebSocket;
using NadekoBot.Attributes; using NadekoBot.Attributes;
using NadekoBot.Extensions;
using NadekoBot.Services; using NadekoBot.Services;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models; using NadekoBot.Services.Database.Models;
@ -47,7 +48,7 @@ namespace NadekoBot.Modules.Utility
foreach (var r in reminders) foreach (var r in reminders)
{ {
var t = StartReminder(r); try { var t = StartReminder(r); } catch (Exception ex) { _log.Warn(ex); }
} }
} }
@ -78,6 +79,7 @@ namespace NadekoBot.Modules.Utility
await ch.SendMessageAsync( await ch.SendMessageAsync(
replacements.Aggregate(RemindMessageFormat, replacements.Aggregate(RemindMessageFormat,
(cur, replace) => cur.Replace(replace.Key, replace.Value(r))) (cur, replace) => cur.Replace(replace.Key, replace.Value(r)))
.SanitizeMentions()
).ConfigureAwait(false); //it works trust me ).ConfigureAwait(false); //it works trust me
} }
catch (Exception ex) { _log.Warn(ex); } catch (Exception ex) { _log.Warn(ex); }
@ -179,7 +181,7 @@ namespace NadekoBot.Modules.Utility
await uow.CompleteAsync(); await uow.CompleteAsync();
} }
try { await channel.SendMessageAsync($"⏰ I will remind \"{(ch is ITextChannel ? ((ITextChannel)ch).Name : umsg.Author.Username)}\" to \"{message.ToString()}\" in {output}. ({time:d.M.yyyy.} at {time:HH:mm})").ConfigureAwait(false); } catch { } try { await channel.SendMessageAsync($"⏰ I will remind \"{(ch is ITextChannel ? ((ITextChannel)ch).Name : umsg.Author.Username)}\" to \"{message.SanitizeMentions()}\" in {output}. ({time:d.M.yyyy.} at {time:HH:mm})").ConfigureAwait(false); } catch { }
await StartReminder(rem); await StartReminder(rem);
} }

View File

@ -45,7 +45,7 @@ namespace NadekoBot.Modules.Utility
[NadekoCommand, Usage, Description, Aliases] [NadekoCommand, Usage, Description, Aliases]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task InRole(IUserMessage umsg, [Remainder] string roles = null) public async Task InRole(IUserMessage umsg, [Remainder] string roles)
{ {
if (string.IsNullOrWhiteSpace(roles)) if (string.IsNullOrWhiteSpace(roles))
return; return;
@ -122,11 +122,11 @@ namespace NadekoBot.Modules.Utility
var guild = channel.Guild; var guild = channel.Guild;
if (target != null) if (target != null)
{ {
await msg.Reply($"`List of roles for **{target.Username}**:` \n• " + string.Join("\n• ", target.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r => r.Position))); await msg.Reply($"`List of roles for **{target.Username}**:` \n• " + string.Join("\n• ", target.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r => r.Position)).SanitizeMentions());
} }
else else
{ {
await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", guild.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r=>r.Position))); await msg.Reply("`List of roles:` \n• " + string.Join("\n• ", guild.Roles.Except(new[] { guild.EveryoneRole }).OrderBy(r=>r.Position)).SanitizeMentions());
} }
} }

View File

@ -23,6 +23,9 @@ namespace NadekoBot.Extensions
http.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); http.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
} }
public static string SanitizeMentions(this string str) =>
str.Replace("@everyone", "@everyοne").Replace("@here", "@һere");
public static double UnixTimestamp(this DateTime dt) => dt.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds; public static double UnixTimestamp(this DateTime dt) => dt.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
public static async Task<IUserMessage> SendMessageAsync(this IGuildUser user, string message, bool isTTS = false) => public static async Task<IUserMessage> SendMessageAsync(this IGuildUser user, string message, bool isTTS = false) =>