Skip to content

Commit

Permalink
Fun cmds show a searching site... while they search
Browse files Browse the repository at this point in the history
Also added cooldowns to YouTube, Google, Giphy
  • Loading branch information
Voltstro committed Nov 1, 2019
1 parent 1ff2a7f commit ce525ab
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 37 deletions.
23 changes: 14 additions & 9 deletions Pootis-Bot/Modules/Fun/GiphySearch.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Rest;
using Pootis_Bot.Core;
using Pootis_Bot.Preconditions;
using Pootis_Bot.Services.Fun;
using Pootis_Bot.Structs;

Expand All @@ -17,9 +19,10 @@ public class GiphySearch : ModuleBase<SocketCommandContext>
[Command("giphy")]
[Summary("Searches Giphy")]
[Alias("gy")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.EmbedLinks)]
[RequireBotPermission(GuildPermission.AttachFiles)]
public async Task CmdGiphySearch([Remainder] string search = "")
public async Task Giphy([Remainder] string search = "")
{
if (string.IsNullOrWhiteSpace(Config.bot.Apis.ApiGiphyKey))
{
Expand All @@ -33,12 +36,18 @@ public async Task CmdGiphySearch([Remainder] string search = "")
return;
}

EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"Giphy Search '{search}'");
embed.WithDescription("Searching Giphy...");
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();
embed.WithColor(FunCmdsConfig.giphyColor);

RestUserMessage message = await Context.Channel.SendMessageAsync("", false, embed.Build());

GiphySearchResult results = GiphyService.Search(search);
if (!results.IsSuccessful)
{
//This should never happen, since we check it at the start!
if (results.ErrorReason == ErrorReason.NoApiKey)
return;
if (results.ErrorReason == ErrorReason.Error)
{
await Context.Channel.SendMessageAsync(
Expand All @@ -47,15 +56,11 @@ await Context.Channel.SendMessageAsync(
}
}

EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"Giphy Search '{Global.Title(results.Data.GifTitle)}'");
embed.WithDescription($"**By**: {results.Data.GifAuthor}\n**URL**: {results.Data.GifLink}");
embed.WithImageUrl(results.Data.GifUrl);
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();
embed.WithColor(FunCmdsConfig.giphyColor);

await Context.Channel.SendMessageAsync("", false, embed.Build());
await message.ModifyAsync(x => { x.Embed = embed.Build(); });
}
}
}
32 changes: 21 additions & 11 deletions Pootis-Bot/Modules/Fun/GoogleSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Rest;
using Discord.WebSocket;
using Google.Apis.Customsearch.v1.Data;
using Pootis_Bot.Core;
using Pootis_Bot.Preconditions;
using Pootis_Bot.Services.Google;

namespace Pootis_Bot.Modules.Fun
Expand All @@ -18,6 +21,7 @@ public class GoogleSearch : ModuleBase<SocketCommandContext>
[Command("google")]
[Summary("Searches Google")]
[Alias("g")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.EmbedLinks)]
public async Task Google([Remainder] string search = "")
{
Expand All @@ -34,12 +38,13 @@ public async Task Google([Remainder] string search = "")
return;
}

await Context.Channel.SendMessageAsync("", false, GSearch(search));
await GSearch(search, Context.Channel);
}

[Command("google")]
[Summary("Searches Google")]
[Alias("g")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.EmbedLinks)]
public async Task Google(int maxSearchResults = 10, [Remainder] string search = "")
{
Expand All @@ -63,11 +68,20 @@ await Context.Channel.SendMessageAsync(
return;
}

await Context.Channel.SendMessageAsync("", false, GSearch(search, maxSearchResults));
await GSearch(search, Context.Channel, maxSearchResults);
}

private Embed GSearch(string search, int maxResults = 10)
private async Task GSearch(string search, ISocketMessageChannel channel, int maxResults = 10)
{
EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"Google Search '{search}'");
embed.WithDescription("Searching Google...");
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithColor(FunCmdsConfig.googleColor);
embed.WithCurrentTimestamp();

RestUserMessage message = await channel.SendMessageAsync("", false, embed.Build());

Search searchListResponse = GoogleService.Search(search, GetType().ToString());

StringBuilder description = new StringBuilder();
Expand All @@ -77,26 +91,22 @@ private Embed GSearch(string search, int maxResults = 10)
{
if (currentResult == maxResults) continue;

string message = $"**[{result.Title}]({result.Link})**\n{result.Snippet}\n\n";
string link = $"**[{result.Title}]({result.Link})**\n{result.Snippet}\n\n";

if (description.Length >= 2048)
continue;

if (description.Length + message.Length >= 2048)
if (description.Length + link.Length >= 2048)
continue;

description.Append(message);
description.Append(link);
currentResult += 1;
}

EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"Google Search '{search}'");
embed.WithDescription(description.ToString());
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithColor(FunCmdsConfig.googleColor);
embed.WithCurrentTimestamp();

return embed.Build();
await message.ModifyAsync(x => { x.Embed = embed.Build(); });
}
}
}
27 changes: 18 additions & 9 deletions Pootis-Bot/Modules/Fun/WikipediaSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
using CreepysinStudios.WikiDotNet;
using Discord;
using Discord.Commands;
using Discord.Rest;
using Discord.WebSocket;
using Pootis_Bot.Preconditions;

namespace Pootis_Bot.Modules.Fun
{
Expand All @@ -13,13 +16,14 @@ public class WikipediaSearch : ModuleBase<SocketCommandContext>
// Description - Wikipedia search
// Contributors - Creepysin,

//This use a library called Wiki.Net
//This uses a library called Wiki.Net
//It was developed by my good friend EternalClickbait and partially by me(Creepysin).
//You can get it here: https://github.com/Creepysin-Studios/Wiki.Net

[Command("wiki")]
[Alias("wikipedia")]
[Summary("Searches Wikipedia")]
[Cooldown(5)]
public async Task Wikipedia([Remainder] string search = "")
{
if (string.IsNullOrWhiteSpace(search))
Expand All @@ -28,12 +32,13 @@ public async Task Wikipedia([Remainder] string search = "")
return;
}

await Context.Channel.SendMessageAsync("", false, WikiSearch(search));
await WikiSearch(search, Context.Channel);
}

[Command("wiki")]
[Alias("wikipedia")]
[Summary("Searches Wikipedia")]
[Cooldown(5)]
public async Task Wikipedia(int maxSearchResults = 15, [Remainder] string search = "")
{
if (string.IsNullOrWhiteSpace(search))
Expand All @@ -49,16 +54,21 @@ await Context.Channel.SendMessageAsync(
return;
}

await Context.Channel.SendMessageAsync("", false, WikiSearch(search, maxSearchResults));
await WikiSearch(search, Context.Channel, maxSearchResults);
}

private Embed WikiSearch(string search, int maxSearch = 10)
private async Task WikiSearch(string search, ISocketMessageChannel channel, int maxSearch = 10)
{
EmbedBuilder embed = new EmbedBuilder();

StringBuilder sb = new StringBuilder();
embed.WithTitle($"Wikipedia Search '{search}'");
embed.WithColor(FunCmdsConfig.wikipediaSearchColor);
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();
embed.WithDescription("Searching Wikipedia...");

RestUserMessage message = await channel.SendMessageAsync("", false, embed.Build());

WikiSearchResponse response = WikiSearcher.Search(search, new WikiSearchSettings
{
Expand All @@ -67,7 +77,7 @@ private Embed WikiSearch(string search, int maxSearch = 10)

foreach (WikiSearchResult result in response.Query.SearchResults)
{
string message =
string link =
$"**[{result.Title}]({result.ConstantUrl})** (Words: {result.WordCount})\n{result.Preview}\n\n";

//There is a character limit of 2048, so lets make sure we don't hit that
Expand All @@ -76,19 +86,18 @@ private Embed WikiSearch(string search, int maxSearch = 10)
continue;
}

if (sb.Length + message.Length >= 2048)
if (sb.Length + link.Length >= 2048)
{
continue;
}

sb.Append(message);
sb.Append(link);
}

embed.WithDescription(sb.ToString());
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();

return embed.Build();
await message.ModifyAsync(x => { x.Embed = embed.Build(); });
}
}
}
28 changes: 20 additions & 8 deletions Pootis-Bot/Modules/Fun/YoutubeSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.Rest;
using Discord.WebSocket;
using Google.Apis.YouTube.v3.Data;
using Pootis_Bot.Core;
using Pootis_Bot.Preconditions;
using Pootis_Bot.Services.Audio;
using Pootis_Bot.Services.Google;
using SearchResult = Google.Apis.YouTube.v3.Data.SearchResult;
Expand All @@ -20,6 +23,7 @@ public class YoutubeSearch : ModuleBase<SocketCommandContext>
[Command("youtube")]
[Summary("Searches Youtube")]
[Alias("yt")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.EmbedLinks)]
public async Task Youtube([Remainder] string search = "")
{
Expand All @@ -35,12 +39,15 @@ public async Task Youtube([Remainder] string search = "")
return;
}

await Context.Channel.SendMessageAsync("", false, YtSearch(search));
await YtSearch(search, Context.Channel);

//await Context.Channel.SendMessageAsync("", false, YtSearch(search));
}

[Command("youtube")]
[Summary("Searches Youtube")]
[Alias("yt")]
[Cooldown(5)]
[RequireBotPermission(GuildPermission.EmbedLinks)]
public async Task Youtube(int maxSearchResults = 6, [Remainder] string search = "")
{
Expand All @@ -63,11 +70,20 @@ await Context.Channel.SendMessageAsync(
return;
}

await Context.Channel.SendMessageAsync("", false, YtSearch(search, maxSearchResults));
await YtSearch(search, Context.Channel, maxSearchResults);
}

private Embed YtSearch(string search, int maxSearch = 6)
private async Task YtSearch(string search, ISocketMessageChannel channel, int maxSearch = 6)
{
EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"YouTube Search '{search}'");
embed.WithDescription("Searching YouTube...");
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();
embed.WithColor(FunCmdsConfig.youtubeColor);

RestUserMessage message = await channel.SendMessageAsync("", false, embed.Build());

//Search Youtube
SearchListResponse searchListResponse = YoutubeService.Search(search, GetType().ToString(), maxSearch);

Expand All @@ -92,14 +108,10 @@ private Embed YtSearch(string search, int maxSearch = 6)
}
}

EmbedBuilder embed = new EmbedBuilder();
embed.WithTitle($"YouTube Search '{search}'");
embed.WithDescription($"**Videos**\n{videos}\n\n**Channels**\n{channels}");
embed.WithFooter($"Search by {Context.User}", Context.User.GetAvatarUrl());
embed.WithCurrentTimestamp();
embed.WithColor(FunCmdsConfig.youtubeColor);

return embed.Build();
await message.ModifyAsync(x => { x.Embed = embed.Build(); });
}
}
}

0 comments on commit ce525ab

Please sign in to comment.