public async Task Send(string where, [Remainder] string msg = "") { if (string.IsNullOrWhiteSpace(msg)) { return; } var destinationMatch = Regex.Match(where, "\\A(?<serverId>\\d+)\\|(?<channelOrUser>c|u):(?<channelOrUserId>\\d+)\\z", RegexOptions.IgnoreCase); if (destinationMatch.Success && ulong.TryParse(destinationMatch.Groups["serverId"].Value, out var destinationServerId) && ulong.TryParse(destinationMatch.Groups["channelOrUserId"].Value, out var destinationChannelOrUserId)) { var channelOrUser = destinationMatch.Groups["channelOrUser"].Value; var guild = _client.Guilds.FirstOrDefault(g => g.Id == destinationServerId); if (guild != null) { var rep = new ReplacementBuilder().WithDefault(Context).Build(); if (channelOrUser.Equals("c", StringComparison.OrdinalIgnoreCase)) { var channel = guild.TextChannels.FirstOrDefault(c => c.Id == destinationChannelOrUserId); if (channel != null) { if (CREmbed.TryParse(msg, out var crembed)) { rep.Replace(crembed); await channel.EmbedAsync(crembed.ToEmbedBuilder(), crembed.PlainText?.SanitizeMentions() ?? "").ConfigureAwait(false); } else { await channel.SendMessageAsync($"{rep.Replace(msg)?.SanitizeMentions() ?? ""}"); } await ReplyConfirmLocalized("message_sent").ConfigureAwait(false); } } else if (channelOrUser.Equals("u", StringComparison.OrdinalIgnoreCase)) { var user = guild.Users.FirstOrDefault(u => u.Id == destinationChannelOrUserId); if (user != null) { if (CREmbed.TryParse(msg, out var crembed)) { rep.Replace(crembed); await(await user.CreateDMChannelAsync()).EmbedAsync(crembed.ToEmbedBuilder(), crembed.PlainText?.SanitizeMentions() ?? "").ConfigureAwait(false); } else { await(await user.CreateDMChannelAsync()).SendMessageAsync($"`#{msg}` {rep.Replace(msg)?.SanitizeMentions() ?? ""}"); } await ReplyConfirmLocalized("message_sent").ConfigureAwait(false); } } } } else { await ReplyErrorLocalized("invalid_format").ConfigureAwait(false); } }
public async Task Send(string where, [Remainder] string msg = null) { if (string.IsNullOrWhiteSpace(msg)) { return; } var ids = where.Split('|'); if (ids.Length != 2) { return; } var sid = ulong.Parse(ids[0]); var server = _client.Guilds.FirstOrDefault(s => s.Id == sid); if (server == null) { return; } var rep = new ReplacementBuilder() .WithDefault(Context) .Build(); if (ids[1].ToUpperInvariant().StartsWith("C:")) { var cid = ulong.Parse(ids[1].Substring(2)); var ch = server.TextChannels.FirstOrDefault(c => c.Id == cid); if (ch == null) { return; } if (CREmbed.TryParse(msg, out var crembed)) { rep.Replace(crembed); await ch.EmbedAsync(crembed.ToEmbed(), $"`#{Context.User}` 📣 " + crembed.PlainText?.SanitizeMentions()) .ConfigureAwait(false); await ReplyConfirmLocalized("message_sent").ConfigureAwait(false); return; } await ch.SendMessageAsync($"**{Context.User}** 📣 " + rep.Replace(msg)?.SanitizeMentions()); } else if (ids[1].ToUpperInvariant().StartsWith("U:")) { var uid = ulong.Parse(ids[1].Substring(2)); var user = server.Users.FirstOrDefault(u => u.Id == uid); if (user == null) { return; } if (CREmbed.TryParse(msg, out var crembed)) { rep.Replace(crembed); await(await user.GetOrCreateDMChannelAsync()).EmbedAsync(crembed.ToEmbed(), $"`{Context.User}` 📣 " + crembed.PlainText?.SanitizeMentions()) .ConfigureAwait(false); await ReplyConfirmLocalized("message_sent").ConfigureAwait(false); return; } await(await user.GetOrCreateDMChannelAsync()).SendMessageAsync($"`{Context.User}` 📣 " + rep.Replace(msg)?.SanitizeMentions()); } else { await ReplyErrorLocalized("invalid_format").ConfigureAwait(false); return; } await ReplyConfirmLocalized("message_sent").ConfigureAwait(false); }