Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
overall code improvements+add one important header in GithubRest.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
InFTord committed Jun 10, 2023
1 parent 76dc0a4 commit 9d352fd
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 28 deletions.
19 changes: 10 additions & 9 deletions DSharpPlusDocs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.3.0" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.3.0" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.3.0" />
<PackageReference Include="DSharpPlus.Rest" Version="4.3.0" />
<PackageReference Include="DSharpPlus.VoiceNext" Version="4.3.0" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.3.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.4.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="DSharpPlus" Version="4.4.1" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.1" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.1" />
<PackageReference Include="DSharpPlus.Rest" Version="4.4.1" />
<PackageReference Include="DSharpPlus.VoiceNext" Version="4.4.1" />
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.1" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
</Project>
12 changes: 5 additions & 7 deletions Modules/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ public class GeneralCommands : BaseCommandModule
[Description("Delete all the messages from this bot within the last X messages")]
public async Task CleanAsync(CommandContext ctx, int messages = 30)
{
if (messages > 50)
messages = messages switch
{
messages = 50;
}
else if (messages < 2)
{
messages = 2;
}
> 50 => 50,
< 2 => 2,
_ => messages
};

IEnumerable<DiscordMessage> msgs = await ctx.Channel.GetMessagesAsync(messages);
msgs = msgs.Where(x => x.Author.Id == ctx.Client.CurrentUser.Id);
Expand Down
2 changes: 1 addition & 1 deletion Paginator/PaginationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public PaginationService(DiscordClient client)
/// </summary>
/// <param name="channel">The channel this message should be sent to</param>
/// <param name="paginated">A <see cref="PaginatedMessage">PaginatedMessage</see> containing the pages.</param>
/// <exception cref="Net.HttpException">Thrown if the bot user cannot send a message or add reactions.</exception>
/// <exception cref="System.Web.HttpException">Thrown if the bot user cannot send a message or add reactions.</exception>
/// <returns>The paginated message.</returns>
public async Task<DiscordMessage> SendPaginatedMessageAsync(DiscordChannel channel, PaginatedMessage paginated)
{
Expand Down
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

namespace DSharpPlusDocs
{
public class Program : IDisposable
public abstract class Program : IDisposable
{
private static DiscordClient _client;
private static MainHandler _mainHandler;
Expand All @@ -46,7 +46,7 @@ public static async Task Main()

_client.Ready += (client, eventArgs) =>
{
Console.WriteLine("Connected!");
_client.Logger.LogInformation("Connected!");
return Task.CompletedTask;
};

Expand Down
2 changes: 1 addition & 1 deletion Query/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ private void LoadType(Type type)
IEnumerable<PropertyInfo> rt = type.GetRuntimeProperties();
foreach (PropertyInfo pi in type.GetRuntimeProperties())
{
if ((pi.GetMethod.IsFamily || pi.GetMethod.IsPublic) && !cb.Properties.Any(x => x.Name == pi.Name))
if ((pi.GetMethod.IsFamily || pi.GetMethod.IsPublic) && cb.Properties.All(x => x.Name != pi.Name))
{
cb.Properties.Add(pi);
}
Expand Down
2 changes: 1 addition & 1 deletion Query/Extensions/TypeDisplay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private async Task<DiscordEmbedBuilder> ShowTypesAsync(DiscordEmbedBuilder eb, I
Console.WriteLine(e.ToString());
result = new DocsHttpResult($"{QueryHandler.DocsBaseUrl}api/{pageUrl}.html");
}
eb.WithAuthor($"{(first.TypeInfo.IsInterface ? "Interface" : (first.TypeInfo.IsEnum ? "Enum" : "Type"))}: {first.TypeInfo.Namespace}.{first.DisplayName}", result.Url, "http://i.imgur.com/yYiUhdi.png");
eb.WithAuthor($"{(first.TypeInfo.IsInterface ? "Interface" : first.TypeInfo.IsEnum ? "Enum" : "Type")}: {first.TypeInfo.Namespace}.{first.DisplayName}", result.Url, "http://i.imgur.com/yYiUhdi.png");
eb.AddField("Docs:", FormatDocsUrl(result.Url), true);
string githubUrl = await GithubRest.GetTypeUrlAsync(first);
if (githubUrl != null)
Expand Down
19 changes: 13 additions & 6 deletions Rest/GithubRest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,28 @@ public class GithubRest
private const string ApiUrl = "https://api.github.com";
private const string AcceptHeader = "application/vnd.github.v3+json";

private static async Task<JObject> SendRequestAsync(HttpMethod method, string endpoint, string extra = null)
private static async Task<JObject> SendRequestAsync(HttpMethod method, string endpoint, string extra = null, string acceptHeader = null)
{
using HttpClient http = new();
HttpRequestMessage request = new(method, $"{ApiUrl}{endpoint}{extra}");
request.Headers.Add("Accept", AcceptHeader);
if (acceptHeader == null)
{
acceptHeader = "application/vnd.github.v3+json";
}

request.Headers.Add("Accept", acceptHeader);
request.Headers.Add("User-Agent", "DSharpPlus Docs Bot/1.0");
request.Headers.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("GITHUB_TOKEN")}");
HttpResponseMessage response = await http.SendAsync(request);
return response.IsSuccessStatusCode
? JObject.Parse(await response.Content.ReadAsStringAsync())
: throw new InvalidOperationException($"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync()}");
}

public static async Task<List<GitSearchResult>> SearchAsync(string search, string filename = null)
private static async Task<List<GitSearchResult>> SearchAsync(string search, string filename = null)
{
string extra = $"?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100";
JObject result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra);
string extra = $"?q={search.Replace(' ', '+')}+repo:DSharpPlus/DSharpPlus+language:c#+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100";
JObject result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra, "application/vnd.github+json");
JArray items = (JArray)result["items"];
List<GitSearchResult> list = new();
foreach (JToken item in items)
Expand All @@ -67,7 +73,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
int pages = (int)Math.Floor(totalCount / 100f);
for (int i = 2; i <= pages + 1; i++)
{
extra = $"?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100&page={i}";
extra = $"?q={search.Replace(' ', '+')}+repo:DSharpPlus/DSharpPlus+?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100&page={i}";
result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra);
items = (JArray)result["items"];
foreach (JToken item in items)
Expand All @@ -76,6 +82,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
}
}
}

return list;
}

Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ services:
tomoe:
build: .
environment:
- "DISCORD_TOKEN": ""
"DISCORD_TOKEN": ""
"GITHUB_TOKEN": ""
restart: unless-stopped

0 comments on commit 9d352fd

Please sign in to comment.