Possible fixes for buggy .bye behavior

This commit is contained in:
Toastie (DCS Team) 2024-10-03 17:51:01 +13:00
parent b506b4461b
commit de97213046
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
4 changed files with 53 additions and 150 deletions

View file

@ -1,52 +0,0 @@
syntax = "proto3";
option csharp_namespace = "EllieBot.GrpcApi";
package info;
service GrpcInfo {
rpc GetServerInfo(ServerInfoRequest) returns (GetServerInfoReply);
}
message ServerInfoRequest {
uint64 guildId = 1;
}
message GetServerInfoReply {
uint64 id = 1;
string name = 2;
string iconUrl = 3;
uint64 ownerId = 4;
string ownerName = 5;
repeated RoleReply roles = 6;
repeated EmojiReply emojis = 7;
repeated string features = 8;
int32 textChannels = 9;
int32 voiceChannels = 10;
int32 memberCount = 11;
int64 createdAt = 12;
}
message RoleReply {
uint64 id = 1;
string name = 2;
string iconUrl = 3;
string color = 4;
}
message EmojiReply {
string name = 1;
string url = 2;
string code = 3;
}
message ChannelReply {
uint64 id = 1;
string name = 2;
ChannelType type = 3;
}
enum ChannelType {
Text = 0;
Voice = 1;
}

View file

@ -93,6 +93,9 @@ public class GreetService : IEService, IReadyExecutor
private Task ClientOnGuildMemberUpdated(Cacheable<SocketGuildUser, ulong> optOldUser, SocketGuildUser newUser)
{
if (!_enabled[GreetType.Boost].Contains(newUser.Guild.Id))
return Task.CompletedTask;
// if user is a new booster
// or boosted again the same server
if ((optOldUser.Value is { PremiumSince: null } && newUser is { PremiumSince: not null })
@ -134,21 +137,63 @@ public class GreetService : IEService, IReadyExecutor
.DeleteAsync();
}
private Task OnUserLeft(SocketGuild guild, SocketUser user)
private Task OnUserJoined(IGuildUser user)
{
_ = Task.Run(async () =>
{
try
{
var conf = await GetGreetSettingsAsync(guild.Id, GreetType.Bye);
if (_enabled[GreetType.Greet].Contains(user.GuildId))
{
var conf = await GetGreetSettingsAsync(user.GuildId, GreetType.Greet);
if (conf?.ChannelId is ulong cid)
{
var channel = await user.Guild.GetTextChannelAsync(cid);
if (channel is not null)
{
await _greetQueue.Writer.WriteAsync((conf, user, channel));
}
}
}
if (conf is null)
if (_enabled[GreetType.GreetDm].Contains(user.GuildId))
{
var confDm = await GetGreetSettingsAsync(user.GuildId, GreetType.GreetDm);
if (confDm is not null)
{
await _greetQueue.Writer.WriteAsync((confDm, user, null));
}
}
}
catch (Exception ex)
{
Log.Error(ex, "Error in GreetService.OnUserJoined. This should not happen. Please report it");
}
});
return Task.CompletedTask;
}
private Task OnUserLeft(SocketGuild guild, SocketUser user)
{
_ = Task.Run(async () =>
{
if (!_enabled[GreetType.Bye].Contains(guild.Id))
return;
var channel = guild.TextChannels.FirstOrDefault(c => c.Id == conf.ChannelId);
try
{
var conf = await GetGreetSettingsAsync(guild.Id, GreetType.Bye);
if (conf?.ChannelId is not { } cid)
return;
var channel = guild.GetChannel(cid) as ITextChannel;
if (channel is null) //maybe warn the server owner that the channel is missing
{
Log.Warning("Channel {ChannelId} in {GuildId} was not found. Bye message will be disabled",
conf.ChannelId,
conf.GuildId);
await SetGreet(guild.Id, null, GreetType.Bye, false);
return;
}
@ -216,9 +261,10 @@ public class GreetService : IEService, IReadyExecutor
or DiscordErrorCode.UnknownChannel)
{
Log.Warning(ex,
"Missing permissions to send a bye message, the greet message will be disabled on server: {GuildId}",
"Missing permissions to send a {GreetType} message, it will be disabled on server: {GuildId}",
conf.GreetType,
channel.GuildId);
await SetGreet(channel.GuildId, channel.Id, GreetType.Greet, false);
await SetGreet(channel.GuildId, channel.Id, conf.GreetType, false);
}
catch (Exception ex)
{
@ -307,43 +353,6 @@ public class GreetService : IEService, IReadyExecutor
IconUrl = user.Guild.IconUrl
};
private Task OnUserJoined(IGuildUser user)
{
_ = Task.Run(async () =>
{
try
{
if (_enabled[GreetType.Greet].Contains(user.GuildId))
{
var conf = await GetGreetSettingsAsync(user.GuildId, GreetType.Greet);
if (conf?.ChannelId is ulong cid)
{
var channel = await user.Guild.GetTextChannelAsync(cid);
if (channel is not null)
{
await _greetQueue.Writer.WriteAsync((conf, user, channel));
}
}
}
if (_enabled[GreetType.GreetDm].Contains(user.GuildId))
{
var confDm = await GetGreetSettingsAsync(user.GuildId, GreetType.GreetDm);
if (confDm is not null)
{
await _greetQueue.Writer.WriteAsync((confDm, user, null));
}
}
}
catch(Exception ex)
{
Log.Error(ex, "Error in GreetService.OnUserJoined. This should not happen. Please report it");
}
});
return Task.CompletedTask;
}
public static string GetDefaultGreet(GreetType greetType)
=> greetType switch

View file

@ -1,50 +0,0 @@
using EllieBot.GrpcApi;
using Grpc.Core;
namespace EllieBot.GrpcApi;
public sealed class ServerInfoSvc : GrpcInfo.GrpcInfoBase, IEService
{
private readonly IStatsService _stats;
public ServerInfoSvc(IStatsService stats)
{
_stats = stats;
}
public override Task<GetServerInfoReply> GetServerInfo(ServerInfoRequest request, ServerCallContext context)
{
var info = _stats.GetGuildInfo(request.GuildId);
var reply = new GetServerInfoReply()
{
Id = info.Id,
Name = info.Name,
IconUrl = info.IconUrl,
OwnerId = info.OwnerId,
OwnerName = info.Owner,
TextChannels = info.TextChannels,
VoiceChannels = info.VoiceChannels,
MemberCount = info.MemberCount,
CreatedAt = info.CreatedAt.Ticks,
};
reply.Features.AddRange(info.Features);
reply.Emojis.AddRange(info.Emojis.Select(x => new EmojiReply()
{
Name = x.Name,
Url = x.Url,
Code = x.ToString()
}));
reply.Roles.AddRange(info.Roles.Select(x => new RoleReply()
{
Id = x.Id,
Name = x.Name,
IconUrl = x.GetIconUrl() ?? string.Empty,
Color = x.Color.ToString()
}));
return Task.FromResult(reply);
}
}

View file

@ -15,18 +15,15 @@ public class GrpcApiService : IEService, IReadyExecutor
private readonly OtherSvc _other;
private readonly ExprsSvc _exprs;
private readonly ServerInfoSvc _info;
private readonly GreetByeSvc _greet;
public GrpcApiService(
OtherSvc other,
ExprsSvc exprs,
ServerInfoSvc info,
GreetByeSvc greet)
{
_other = other;
_exprs = exprs;
_info = info;
_greet = greet;
}
@ -43,7 +40,6 @@ public class GrpcApiService : IEService, IReadyExecutor
{
GrpcOther.BindService(_other),
GrpcExprs.BindService(_exprs),
GrpcInfo.BindService(_info),
GrpcGreet.BindService(_greet)
},
Ports =