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}");
|
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)]
|
[Command("modchannel", RunMode = RunMode.Async)]
|
||||||
[Summary("Set a channel for moderation purposes")]
|
[Summary("Set a channel for moderation purposes")]
|
||||||
public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel)
|
public async Task SelectModChannel([Summary("#Channel")] ISocketMessageChannel channel)
|
||||||
|
@ -67,7 +90,7 @@ namespace Geekbot.net.Commands.Admin
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ namespace Geekbot.net.Database.Models
|
||||||
{
|
{
|
||||||
[Key]
|
[Key]
|
||||||
public int Id { get; set; }
|
public int Id { get; set; }
|
||||||
|
|
||||||
[Required]
|
[Required]
|
||||||
public long GuildId { get; set; }
|
public long GuildId { get; set; }
|
||||||
|
|
||||||
|
@ -15,9 +15,11 @@ namespace Geekbot.net.Database.Models
|
||||||
public bool Hui { get; set; } = false;
|
public bool Hui { get; set; } = false;
|
||||||
|
|
||||||
public long ModChannel { get; set; } = 0;
|
public long ModChannel { get; set; } = 0;
|
||||||
|
|
||||||
public string WelcomeMessage { get; set; }
|
public string WelcomeMessage { get; set; }
|
||||||
|
|
||||||
|
public long WelcomeChannel { get; set; }
|
||||||
|
|
||||||
public bool ShowDelete { get; set; } = false;
|
public bool ShowDelete { get; set; } = false;
|
||||||
|
|
||||||
public bool ShowLeave { get; set; } = false;
|
public bool ShowLeave { get; set; } = false;
|
||||||
|
|
|
@ -4,6 +4,7 @@ using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
using Discord;
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
using Discord.Rest;
|
||||||
using Discord.WebSocket;
|
using Discord.WebSocket;
|
||||||
using Geekbot.net.Database;
|
using Geekbot.net.Database;
|
||||||
using Geekbot.net.Database.Models;
|
using Geekbot.net.Database.Models;
|
||||||
|
@ -143,18 +144,38 @@ namespace Geekbot.net
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var userRepoUpdate = _userRepository.Update(user);
|
||||||
|
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
||||||
|
|
||||||
if (!user.IsBot)
|
if (!user.IsBot)
|
||||||
{
|
{
|
||||||
var message = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId.Equals(user.Guild.Id.AsLong()))?.WelcomeMessage;
|
var guildSettings = _database.GuildSettings.FirstOrDefault(guild => guild.GuildId == user.Guild.Id.AsLong());
|
||||||
if (!string.IsNullOrEmpty(message))
|
var message = guildSettings?.WelcomeMessage;
|
||||||
|
if (string.IsNullOrEmpty(message)) return;
|
||||||
|
message = message.Replace("$user", user.Mention);
|
||||||
|
|
||||||
|
var fallbackSender = new Func<Task<RestUserMessage>>(() => user.Guild.DefaultChannel.SendMessageAsync(message));
|
||||||
|
if (guildSettings.WelcomeChannel != 0)
|
||||||
{
|
{
|
||||||
message = message.Replace("$user", user.Mention);
|
try
|
||||||
await user.Guild.DefaultChannel.SendMessageAsync(message);
|
{
|
||||||
|
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);
|
await userRepoUpdate;
|
||||||
_logger.Information(LogSource.Geekbot, $"{user.Username} ({user.Id}) joined {user.Guild.Name} ({user.Guild.Id})");
|
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue