configurable vote endpoints

This commit is contained in:
Toastie 2025-03-29 21:05:43 +13:00
parent 99a8030898
commit d414ecda2d
Signed by: toastie_t0ast
GPG key ID: 0861BE54AD481DC7
4 changed files with 18 additions and 55 deletions
src/EllieBot
Modules/Gambling
_common

View file

@ -1,7 +1,6 @@
using System.Globalization;
using Grpc.Core;
using EllieBot.Common.ModuleBehaviors;
using EllieBot.GrpcApi;
using EllieBot.GrpcVotesApi;
namespace EllieBot.Modules.Gambling.Services;
@ -12,7 +11,8 @@ public class VoteRewardService(
CurrencyService cs,
IBotCache cache,
DiscordSocketClient client,
IMessageSenderService sender
IMessageSenderService sender,
IBotCreds creds
) : IEService, IReadyExecutor
{
private TypedKey<DateTime> VoteKey(ulong userId)
@ -26,6 +26,9 @@ public class VoteRewardService(
if (shardData.ShardId != 0)
return;
if (creds.Votes is null || creds.Votes.Host is null || creds.Votes.Port == 0)
return;
var serverCreds = ServerCredentials.Insecure;
var ssd = VoteService.BindService(new VotesGrpcService(this));
@ -33,7 +36,7 @@ public class VoteRewardService(
{
Ports =
{
new("127.0.0.1", 59384, serverCreds),
new(creds.Votes.Host, creds.Votes.Port, serverCreds),
}
};
@ -44,8 +47,6 @@ public class VoteRewardService(
{
_voteFeedChannel = await client.GetChannelAsync(cid) as IMessageChannel;
}
return;
}
public void SetVoiceChannel(IMessageChannel? channel)
@ -118,7 +119,6 @@ public class VoteRewardService(
public sealed class VotesGrpcService(VoteRewardService vrs)
: VoteService.VoteServiceBase, IEService
{
[GrpcNoAuthRequired]
public override async Task<GrpcVoteResult> VoteReceived(GrpcVoteData request, ServerCallContext context)
{
await vrs.UserVotedAsync(ulong.Parse(request.UserId), request.Type);

View file

@ -35,10 +35,10 @@ public interface IBotCreds
public interface IVotesSettings
{
string TopggServiceUrl { get; set; }
string TopggKey { get; set; }
string DiscordsServiceUrl { get; set; }
string DiscordsKey { get; set; }
string Host { get; set; }
int Port { get; set; }
string DblApiKey { get; set; }
string DiscordsApiKey { get; set; }
}
public interface IPatreonSettings

View file

@ -6,7 +6,7 @@ namespace EllieBot.Common;
public sealed class Creds : IBotCreds
{
[Comment("""DO NOT CHANGE""")]
public int Version { get; set; } = 20;
public int Version { get; set; } = 21;
[Comment("""Bot token. Do not share with anyone ever -> https://discordapp.com/developers/applications/""")]
public string Token { get; set; }
@ -176,7 +176,7 @@ public sealed class Creds : IBotCreds
OwnerIds = new List<ulong>();
TotalShards = 1;
GoogleApiKey = string.Empty;
Votes = new VotesSettings(string.Empty, string.Empty, string.Empty, string.Empty);
Votes = new VotesSettings();
Patreon = new PatreonSettings(string.Empty, string.Empty, string.Empty, string.Empty);
BotListToken = string.Empty;
CleverbotApiKey = string.Empty;
@ -246,47 +246,10 @@ public sealed class Creds : IBotCreds
public sealed record VotesSettings : IVotesSettings
{
[Comment("""
top.gg votes service url
This is the url of your instance of the EllieBot.Votes api
Example: https://votes.my.cool.bot.com
""")]
public string TopggServiceUrl { get; set; }
[Comment("""
Authorization header value sent to the TopGG service url with each request
This should be equivalent to the TopggKey in your EllieBot.Votes api appsettings.json file
""")]
public string TopggKey { get; set; }
[Comment("""
discords.com votes service url
This is the url of your instance of the EllieBot.Votes api
Example: https://votes.my.cool.bot.com
""")]
public string DiscordsServiceUrl { get; set; }
[Comment("""
Authorization header value sent to the Discords service url with each request
This should be equivalent to the DiscordsKey in your EllieBot.Votes api appsettings.json file
""")]
public string DiscordsKey { get; set; }
public VotesSettings()
{
}
public VotesSettings(
string topggServiceUrl,
string topggKey,
string discordsServiceUrl,
string discordsKey)
{
TopggServiceUrl = topggServiceUrl;
TopggKey = topggKey;
DiscordsServiceUrl = discordsServiceUrl;
DiscordsKey = discordsKey;
}
public string Host { get; set; }
public int Port { get; set; }
public string DblApiKey { get; set; }
public string DiscordsApiKey { get; set; }
}
public sealed record GrpcApiConfig

View file

@ -133,9 +133,9 @@ public sealed class BotCredsProvider : IBotCredsProvider
if (File.Exists(CREDS_FILE_NAME))
{
var creds = Yaml.Deserializer.Deserialize<Creds>(File.ReadAllText(CREDS_FILE_NAME));
if (creds.Version < 20)
if (creds.Version < 21)
{
creds.Version = 20;
creds.Version = 21;
File.WriteAllText(CREDS_FILE_NAME, Yaml.Serializer.Serialize(creds));
}
}