Skip to content

Commit

Permalink
Merge pull request #1 from Nikolai558/main
Browse files Browse the repository at this point in the history
v1.1.0-alpha
  • Loading branch information
Nikolai558 authored Apr 13, 2022
2 parents b4b648c + 893c6b7 commit 06d4794
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions FEBuddyDiscordBot/FeBuddyBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ public async Task StartAsync()
var _builder = new ConfigurationBuilder().SetBasePath(AppContext.BaseDirectory).AddJsonFile(path: "config.json");
_config = _builder.Build();

var intents = Discord.GatewayIntents.All;
DiscordSocketConfig discordConfig = new DiscordSocketConfig()
{

GatewayIntents = GatewayIntents.GuildMembers | GatewayIntents.DirectMessages | GatewayIntents.GuildMessages | GatewayIntents.Guilds | GatewayIntents.GuildVoiceStates
};

var services = new ServiceCollection()
.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig { GatewayIntents = intents}))
.AddSingleton(new DiscordSocketClient(discordConfig))
.AddSingleton(_config)
.AddSingleton(new CommandService(new CommandServiceConfig { DefaultRunMode = RunMode.Async, LogLevel = LogSeverity.Debug, CaseSensitiveCommands = false, ThrowOnError = false }))
.AddSingleton<StartupService>()
Expand Down
21 changes: 21 additions & 0 deletions FEBuddyDiscordBot/Services/RoleAssignmentService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,31 @@ public RoleAssignmentService(IServiceProvider services)
_logger = _services.GetRequiredService<ILogger<RoleAssignmentService>>();

_discord.UserJoined += UserJoined;
_discord.UserVoiceStateUpdated += UserConnectedToVoice;

_logger.LogInformation("Loaded: RoleAssignmentService");
}

private async Task UserConnectedToVoice(SocketUser User, SocketVoiceState CurrentVoiceState, SocketVoiceState NewVoiceState)
{
SocketGuildUser _user = (SocketGuildUser)User;

SocketRole voiceMeetingTextRole = _user.Guild.Roles.First(x => x.Name == "voice-meeting-txt");
string privateMeetingVoiceChnlName = "Private Meeting";

if (CurrentVoiceState.VoiceChannel != null && CurrentVoiceState.VoiceChannel.Name == privateMeetingVoiceChnlName)
{
await _user.RemoveRoleAsync(voiceMeetingTextRole);
_logger.LogInformation($"Remove Role: {_user.Username} ({_user.Id}) in {_user.Guild.Name} -> User is no longer connected to {privateMeetingVoiceChnlName} Voice Channel; Removed the {voiceMeetingTextRole.Name} role.");
}

if (NewVoiceState.VoiceChannel != null && NewVoiceState.VoiceChannel.Name == privateMeetingVoiceChnlName)
{
await _user.AddRoleAsync(voiceMeetingTextRole);
_logger.LogInformation($"Give Role: {_user.Username} ({_user.Id}) in {_user.Guild.Name} -> User Connected to {privateMeetingVoiceChnlName} Voice Channel; Added the {voiceMeetingTextRole.Name} role");
}
}

private async Task UserJoined(SocketGuildUser User)
{
_logger.LogInformation($"User Joined: {User.Username} ({User.Id}) joined {User.Guild.Name}");
Expand Down

0 comments on commit 06d4794

Please sign in to comment.