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

Commit 9d352fd

Browse files
overall code improvements+add one important header in GithubRest.cs
1 parent 76dc0a4 commit 9d352fd

File tree

8 files changed

+35
-28
lines changed

8 files changed

+35
-28
lines changed

DSharpPlusDocs.csproj

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@
66
<OutputType>Exe</OutputType>
77
</PropertyGroup>
88
<ItemGroup>
9-
<PackageReference Include="DSharpPlus" Version="4.3.0" />
10-
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.3.0" />
11-
<PackageReference Include="DSharpPlus.Interactivity" Version="4.3.0" />
12-
<PackageReference Include="DSharpPlus.Rest" Version="4.3.0" />
13-
<PackageReference Include="DSharpPlus.VoiceNext" Version="4.3.0" />
14-
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.3.0" />
15-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.4.0" />
16-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.4.0" />
17-
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
9+
<PackageReference Include="DSharpPlus" Version="4.4.1" />
10+
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.4.1" />
11+
<PackageReference Include="DSharpPlus.Interactivity" Version="4.4.1" />
12+
<PackageReference Include="DSharpPlus.Rest" Version="4.4.1" />
13+
<PackageReference Include="DSharpPlus.VoiceNext" Version="4.4.1" />
14+
<PackageReference Include="DSharpPlus.SlashCommands" Version="4.4.1" />
15+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.6.0" />
16+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="4.6.0" />
17+
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="7.0.0" />
18+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
1819
</ItemGroup>
1920
</Project>

Modules/Commands.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,12 @@ public class GeneralCommands : BaseCommandModule
5151
[Description("Delete all the messages from this bot within the last X messages")]
5252
public async Task CleanAsync(CommandContext ctx, int messages = 30)
5353
{
54-
if (messages > 50)
54+
messages = messages switch
5555
{
56-
messages = 50;
57-
}
58-
else if (messages < 2)
59-
{
60-
messages = 2;
61-
}
56+
> 50 => 50,
57+
< 2 => 2,
58+
_ => messages
59+
};
6260

6361
IEnumerable<DiscordMessage> msgs = await ctx.Channel.GetMessagesAsync(messages);
6462
msgs = msgs.Where(x => x.Author.Id == ctx.Client.CurrentUser.Id);

Paginator/PaginationService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public PaginationService(DiscordClient client)
4848
/// </summary>
4949
/// <param name="channel">The channel this message should be sent to</param>
5050
/// <param name="paginated">A <see cref="PaginatedMessage">PaginatedMessage</see> containing the pages.</param>
51-
/// <exception cref="Net.HttpException">Thrown if the bot user cannot send a message or add reactions.</exception>
51+
/// <exception cref="System.Web.HttpException">Thrown if the bot user cannot send a message or add reactions.</exception>
5252
/// <returns>The paginated message.</returns>
5353
public async Task<DiscordMessage> SendPaginatedMessageAsync(DiscordChannel channel, PaginatedMessage paginated)
5454
{

Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
namespace DSharpPlusDocs
3131
{
32-
public class Program : IDisposable
32+
public abstract class Program : IDisposable
3333
{
3434
private static DiscordClient _client;
3535
private static MainHandler _mainHandler;
@@ -46,7 +46,7 @@ public static async Task Main()
4646

4747
_client.Ready += (client, eventArgs) =>
4848
{
49-
Console.WriteLine("Connected!");
49+
_client.Logger.LogInformation("Connected!");
5050
return Task.CompletedTask;
5151
};
5252

Query/Cache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ private void LoadType(Type type)
235235
IEnumerable<PropertyInfo> rt = type.GetRuntimeProperties();
236236
foreach (PropertyInfo pi in type.GetRuntimeProperties())
237237
{
238-
if ((pi.GetMethod.IsFamily || pi.GetMethod.IsPublic) && !cb.Properties.Any(x => x.Name == pi.Name))
238+
if ((pi.GetMethod.IsFamily || pi.GetMethod.IsPublic) && cb.Properties.All(x => x.Name != pi.Name))
239239
{
240240
cb.Properties.Add(pi);
241241
}

Query/Extensions/TypeDisplay.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private async Task<DiscordEmbedBuilder> ShowTypesAsync(DiscordEmbedBuilder eb, I
4949
Console.WriteLine(e.ToString());
5050
result = new DocsHttpResult($"{QueryHandler.DocsBaseUrl}api/{pageUrl}.html");
5151
}
52-
eb.WithAuthor($"{(first.TypeInfo.IsInterface ? "Interface" : (first.TypeInfo.IsEnum ? "Enum" : "Type"))}: {first.TypeInfo.Namespace}.{first.DisplayName}", result.Url, "http://i.imgur.com/yYiUhdi.png");
52+
eb.WithAuthor($"{(first.TypeInfo.IsInterface ? "Interface" : first.TypeInfo.IsEnum ? "Enum" : "Type")}: {first.TypeInfo.Namespace}.{first.DisplayName}", result.Url, "http://i.imgur.com/yYiUhdi.png");
5353
eb.AddField("Docs:", FormatDocsUrl(result.Url), true);
5454
string githubUrl = await GithubRest.GetTypeUrlAsync(first);
5555
if (githubUrl != null)

Rest/GithubRest.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,28 @@ public class GithubRest
3838
private const string ApiUrl = "https://api.github.com";
3939
private const string AcceptHeader = "application/vnd.github.v3+json";
4040

41-
private static async Task<JObject> SendRequestAsync(HttpMethod method, string endpoint, string extra = null)
41+
private static async Task<JObject> SendRequestAsync(HttpMethod method, string endpoint, string extra = null, string acceptHeader = null)
4242
{
4343
using HttpClient http = new();
4444
HttpRequestMessage request = new(method, $"{ApiUrl}{endpoint}{extra}");
45-
request.Headers.Add("Accept", AcceptHeader);
45+
if (acceptHeader == null)
46+
{
47+
acceptHeader = "application/vnd.github.v3+json";
48+
}
49+
50+
request.Headers.Add("Accept", acceptHeader);
4651
request.Headers.Add("User-Agent", "DSharpPlus Docs Bot/1.0");
52+
request.Headers.Add("Authorization", $"Bearer {Environment.GetEnvironmentVariable("GITHUB_TOKEN")}");
4753
HttpResponseMessage response = await http.SendAsync(request);
4854
return response.IsSuccessStatusCode
4955
? JObject.Parse(await response.Content.ReadAsStringAsync())
5056
: throw new InvalidOperationException($"{response.ReasonPhrase}: {await response.Content.ReadAsStringAsync()}");
5157
}
5258

53-
public static async Task<List<GitSearchResult>> SearchAsync(string search, string filename = null)
59+
private static async Task<List<GitSearchResult>> SearchAsync(string search, string filename = null)
5460
{
55-
string extra = $"?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100";
56-
JObject result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra);
61+
string extra = $"?q={search.Replace(' ', '+')}+repo:DSharpPlus/DSharpPlus+language:c#+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100";
62+
JObject result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra, "application/vnd.github+json");
5763
JArray items = (JArray)result["items"];
5864
List<GitSearchResult> list = new();
5965
foreach (JToken item in items)
@@ -67,7 +73,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
6773
int pages = (int)Math.Floor(totalCount / 100f);
6874
for (int i = 2; i <= pages + 1; i++)
6975
{
70-
extra = $"?q=repo:DSharpPlus/DSharpPlus+language:cs+in:file{(filename == null ? "" : $"+filename:{filename}")}+{search.Replace(' ', '+')}&per_page=100&page={i}";
76+
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}";
7177
result = await SendRequestAsync(HttpMethod.Get, "/search/code", extra);
7278
items = (JArray)result["items"];
7379
foreach (JToken item in items)
@@ -76,6 +82,7 @@ public static async Task<List<GitSearchResult>> SearchAsync(string search, strin
7682
}
7783
}
7884
}
85+
7986
return list;
8087
}
8188

docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ services:
44
tomoe:
55
build: .
66
environment:
7-
- "DISCORD_TOKEN": ""
7+
"DISCORD_TOKEN": ""
8+
"GITHUB_TOKEN": ""
89
restart: unless-stopped

0 commit comments

Comments
 (0)