Merge pull request #345 from appelemac/dev

Hotfix rip
This commit is contained in:
Master Kwoth 2016-06-15 16:33:09 +02:00 committed by GitHub
commit a14e3276b0
3 changed files with 132 additions and 66 deletions

View File

@ -0,0 +1,127 @@
using NadekoBot.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using System.Drawing;
using System.Drawing.Drawing2D;
using NadekoBot.Properties;
using System.IO;
using System.Drawing.Imaging;
using NadekoBot.Extensions;
namespace NadekoBot.Modules.Conversations.Commands
{
class RipCommand : DiscordCommand
{
public RipCommand(DiscordModule module) : base(module)
{
}
internal override void Init(CommandGroupBuilder cgb)
{
cgb.CreateCommand("rip")
.Description("Shows a grave image of someone with a start year\n**Usage**: @NadekoBot rip @Someone 2000")
.Parameter("user", ParameterType.Required)
.Parameter("year", ParameterType.Optional)
.Do(async e =>
{
if (string.IsNullOrWhiteSpace(e.GetArg("user")))
return;
var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
var text = "";
Stream file;
if (usr == null)
{
text = e.GetArg("user");
file = RipName(text, string.IsNullOrWhiteSpace(e.GetArg("year"))
? null
: e.GetArg("year"));
} else
{
var avatar = await GetAvatar(usr.AvatarUrl);
text = usr.Name;
file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year"))
? null
: e.GetArg("year"));
}
await e.Channel.SendFile("ripzor_m8.png",
file);
});
}
/// <summary>
/// Create a RIP image of the given name and avatar, with an optional year
/// </summary>
/// <param name="name"></param>
/// <param name="avatar"></param>
/// <param name="year"></param>
/// <returns></returns>
public Stream RipUser(string name, Image avatar, string year = null)
{
var bm = Resources.rip;
var offset = name.Length * 2;
var fontSize = 20;
if (name.Length > 10)
{
fontSize -= (name.Length - 10) / 2;
}
//TODO use measure string
var g = Graphics.FromImage(bm);
g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 220);
g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 240);
g.DrawImage(avatar, 80, 135);
g.DrawImage((Image)Resources.rose_overlay, 0, 0);
g.Flush();
g.Dispose();
return bm.ToStream(ImageFormat.Png);
}
public Stream RipName(string name, string year = null)
{
var bm = Resources.rip;
var offset = name.Length * 5;
var fontSize = 20;
if (name.Length > 10)
{
fontSize -= (name.Length - 10) / 2;
}
//TODO use measure string
var g = Graphics.FromImage(bm);
g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 200);
g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 235);
g.Flush();
g.Dispose();
return bm.ToStream(ImageFormat.Png);
}
public static async Task<Image> GetAvatar(string url)
{
var stream = await SearchHelper.GetResponseStreamAsync(url);
Bitmap bmp = new Bitmap(100, 100);
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddEllipse(0, 0, bmp.Width, bmp.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.SetClip(gp);
gr.DrawImage(Image.FromStream(stream), Point.Empty);
}
}
return bmp;
}
}
}

View File

@ -15,6 +15,7 @@ using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NadekoBot.Modules.Conversations.Commands;
namespace NadekoBot.Modules.Conversations namespace NadekoBot.Modules.Conversations
{ {
@ -25,6 +26,7 @@ namespace NadekoBot.Modules.Conversations
{ {
commands.Add(new CopyCommand(this)); commands.Add(new CopyCommand(this));
commands.Add(new RequestsCommand(this)); commands.Add(new RequestsCommand(this));
commands.Add(new RipCommand(this));
} }
public override string Prefix { get; } = String.Format(NadekoBot.Config.CommandPrefixes.Conversations, NadekoBot.Creds.BotId); public override string Prefix { get; } = String.Format(NadekoBot.Config.CommandPrefixes.Conversations, NadekoBot.Creds.BotId);
@ -186,24 +188,7 @@ namespace NadekoBot.Modules.Conversations
await e.Channel.SendMessage(str).ConfigureAwait(false); await e.Channel.SendMessage(str).ConfigureAwait(false);
}); });
cgb.CreateCommand("rip")
.Description("Shows a grave image of someone with a start year\n**Usage**: @NadekoBot rip @Someone 2000")
.Parameter("user", ParameterType.Required)
.Parameter("year", ParameterType.Optional)
.Do(async e =>
{
if (string.IsNullOrWhiteSpace(e.GetArg("user")))
return;
var usr = e.Channel.FindUsers(e.GetArg("user")).FirstOrDefault();
var text = "";
var avatar = await GetAvatar(usr.AvatarUrl);
text = usr?.Name ?? e.GetArg("user");
var file = RipUser(text, avatar, string.IsNullOrWhiteSpace(e.GetArg("year"))
? null
: e.GetArg("year"));
await e.Channel.SendFile("ripzor_m8.png",
file);
});
if (!NadekoBot.Config.DontJoinServers) if (!NadekoBot.Config.DontJoinServers)
{ {
cgb.CreateCommand("j") cgb.CreateCommand("j")
@ -321,54 +306,7 @@ namespace NadekoBot.Modules.Conversations
} }
/// <summary>
/// Create a RIP image of the given name and avatar, with an optional year
/// </summary>
/// <param name="name"></param>
/// <param name="avatar"></param>
/// <param name="year"></param>
/// <returns></returns>
public Stream RipUser(string name, Image avatar, string year = null)
{
var bm = Resources.rip;
var offset = name.Length * 2;
var fontSize = 20;
if (name.Length > 10)
{
fontSize -= (name.Length - 10) / 2;
}
//TODO use measure string
var g = Graphics.FromImage(bm);
g.DrawString(name, new Font("Comic Sans MS", fontSize, FontStyle.Bold), Brushes.Black, 100 - offset, 220);
g.DrawString((year ?? "?") + " - " + DateTime.Now.Year, new Font("Consolas", 12, FontStyle.Bold), Brushes.Black, 80, 240);
g.DrawImage(avatar, 80, 135);
g.DrawImage((Image)Resources.rose_overlay, 0, 0);
g.Flush();
g.Dispose();
return bm.ToStream(ImageFormat.Png);
}
public static async Task<Image> GetAvatar(string url)
{
var stream = await SearchHelper.GetResponseStreamAsync(url);
Bitmap bmp = new Bitmap(100, 100);
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddEllipse(0, 0, bmp.Width, bmp.Height);
using (Graphics gr = Graphics.FromImage(bmp))
{
gr.SetClip(gp);
gr.DrawImage(Image.FromStream(stream), Point.Empty);
}
}
return bmp;
}
private static Func<CommandEventArgs, Task> SayYes() private static Func<CommandEventArgs, Task> SayYes()
=> async e => await e.Channel.SendMessage("Yes. :)").ConfigureAwait(false); => async e => await e.Channel.SendMessage("Yes. :)").ConfigureAwait(false);
} }

View File

@ -140,6 +140,7 @@
<Compile Include="Modules\ClashOfClans\ClashOfClans.cs" /> <Compile Include="Modules\ClashOfClans\ClashOfClans.cs" />
<Compile Include="Classes\DBHandler.cs" /> <Compile Include="Classes\DBHandler.cs" />
<Compile Include="Classes\FlowersHandler.cs" /> <Compile Include="Classes\FlowersHandler.cs" />
<Compile Include="Modules\Conversations\Commands\RipCommand.cs" />
<Compile Include="Modules\CustomReactions\CustomReactions.cs" /> <Compile Include="Modules\CustomReactions\CustomReactions.cs" />
<Compile Include="Modules\Programming\Commands\HaskellRepl.cs" /> <Compile Include="Modules\Programming\Commands\HaskellRepl.cs" />
<Compile Include="Modules\Programming\ProgrammingModule.cs" /> <Compile Include="Modules\Programming\ProgrammingModule.cs" />