Allow a server admin to specify a channel for the welcome message
This commit is contained in:
parent
8822e65e6a
commit
ced287e492
3 changed files with 55 additions and 9 deletions
|
@ -46,6 +46,29 @@ namespace Geekbot.net.Commands.Admin
|
|||
await ReplyAsync($"Welcome message has been changed\r\nHere is an example of how it would look:\r\n{formatedMessage}");
|
||||
}
|
||||
|
||||
[Command("welcomechannel", RunMode = RunMode.Async)]
|
||||
[Summary("Set a channel for the welcome messages (by default it uses the top most channel)")]
|
||||
public async Task SelectWelcomeChannel([Summary("#Channel")] ISocketMessageChannel channel)
|
||||
{
|
||||
try
|
||||
{
|
||||
var m = await channel.SendMessageAsync("...");
|
||||
|
||||
var guild = await GetGuildSettings(Context.Guild.Id);
|
||||
guild.WelcomeChannel = channel.Id.AsLong();
|
||||
_database.GuildSettings.Update(guild);
|
||||
await _database.SaveChangesAsync();
|
||||
|
||||
await m.DeleteAsync();
|
||||
|
||||
await ReplyAsync("Successfully saved the welcome channel");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions");
|
||||
}
|
||||
}
|
||||
|
||||
[Command("modchannel", RunMode = RunMode.Async)]
|
||||
[Summary("Set a channel for moderation purposes")]
|
||||
public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel)
|
||||
|
@ -67,7 +90,7 @@ namespace Geekbot.net.Commands.Admin
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to be valid");
|
||||
await _errorHandler.HandleCommandException(e, Context, "That channel doesn't seem to exist or i don't have write permissions");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -18,6 +18,8 @@ namespace Geekbot.net.Database.Models
|
|||
|
||||
public string WelcomeMessage { get; set; }
|
||||
|
||||
public long WelcomeChannel { get; set; }
|
||||
|
||||
public bool ShowDelete { get; set; } = false;
|
||||
|
||||
public bool ShowLeave { get; set; } = false;
|
||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
|||
using System.Threading.Tasks;
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using Discord.Rest;
|
||||
using Discord.WebSocket;
|
||||
using Geekbot.net.Database;
|
||||
using Geekbot.net.Database.Models;
|
||||
|
@ -143,18 +144,38 @@ namespace Geekbot.net
|
|||
{
|
||||
try
|
||||
{
|
||||
var userRepoUpdate = _userRepository.Update(user);
|
||||
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
||||
|
||||
if (!user.IsBot)
|
||||
{
|
||||
var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage;
|
||||
if (!string.IsNullOrEmpty(message))
|
||||
{
|
||||
var guildSettings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId == user.Guild.Id.AsLong());
|
||||
var message = guildSettings?.WelcomeMessage;
|
||||
if (string.IsNullOrEmpty(message)) return;
|
||||
message = message.Replace("$user", user.Mention);
|
||||
await user.Guild.DefaultChannel.SendMessageAsync(message);
|
||||
|
||||
var fallbackSender = new Func<Task<RestUserMessage>>(() => user.Guild.DefaultChannel.SendMessageAsync(message));
|
||||
if (guildSettings.WelcomeChannel != 0)
|
||||
{
|
||||
try
|
||||
{
|
||||
var target = await _client.GetChannelAsync(guildSettings.WelcomeChannel.AsUlong());
|
||||
var channel = target as ISocketMessageChannel;
|
||||
await channel.SendMessageAsync(message);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(LogSource.Geekbot, "Failed to send welcome message to user defined welcome channel", e);
|
||||
await fallbackSender();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
await fallbackSender();
|
||||
}
|
||||
}
|
||||
|
||||
await _userRepository.Update(user);
|
||||
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
||||
await userRepoUpdate;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
|
Loading…
Reference in a new issue