Add welcome message and channel

This commit is contained in:
Elara 2023-12-05 17:36:03 -08:00
parent a5933b2a97
commit e90b6809fe
6 changed files with 104 additions and 9 deletions

View File

@ -52,6 +52,8 @@ If a moderator accepts the request, a new ticket will be created in which mods c
- `/vetting role` can be used by anyone with the `Manage Server` permission to set the server's vetting role. owobot will assign this role to all new users.
- `/vetting req_channel` can be used by anyone with the `Manage Server` permission to set the server's vetting request channel. This is where owobot will post vetting requests.
- `/vetting welcome_channel` can be used by anyone with the `Manage Server` permission to set the server's welcome channel. This is where owobot will welcome new users.
- `/vetting welcome_msg` can be used by anyone with the `Manage Server` permission to set the server's welcome message. This is the message owobot will use to welcome new users. You can use `$user` in the message, which will be replaced with a user mention.
- `/approve` can be used by anyone with the `Kick Members` permission to approve users that are in vetting.
### Tickets

View File

@ -33,6 +33,8 @@ type Guild struct {
VettingReqChanID string `db:"vetting_req_chan_id"`
VettingRoleID string `db:"vetting_role_id"`
TimeFormat string `db:"time_format"`
WelcomeChanID string `db:"welcome_chan_id"`
WelcomeMsg string `db:"welcome_msg"`
}
func AllGuilds() ([]Guild, error) {
@ -92,6 +94,16 @@ func SetTimeFormat(guildID, timeFmt string) error {
return err
}
func SetWelcomeChannel(guildID, channelID string) error {
_, err := db.Exec("UPDATE guilds SET welcome_chan_id = ? WHERE id = ?", channelID, guildID)
return err
}
func SetWelcomeMsg(guildID, msg string) error {
_, err := db.Exec("UPDATE guilds SET welcome_msg = ? WHERE id = ?", msg, guildID)
return err
}
func IsVettingMsg(msgID string) (bool, error) {
var out bool
err := db.QueryRow("SELECT 1 FROM guild WHERE vetting_msg_id = ?", msgID).Scan(&out)

View File

@ -12,8 +12,7 @@ CREATE TABLE guilds (
ticket_log_chan_id TEXT NOT NULL DEFAULT '',
ticket_category_id TEXT NOT NULL DEFAULT '',
vetting_req_chan_id TEXT NOT NULL DEFAULT '',
vetting_role_id TEXT NOT NULL DEFAULT '',
time_format TEXT NOT NULL DEFAULT 'discord'
vetting_role_id TEXT NOT NULL DEFAULT ''
);
/* tickets holds all open tickets */

View File

@ -0,0 +1,6 @@
/* add time format column */
ALTER TABLE guilds ADD COLUMN time_format TEXT NOT NULL DEFAULT 'discord';
/* add welcome message columns */
ALTER TABLE guilds ADD COLUMN welcome_chan_id TEXT NOT NULL DEFAULT '';
ALTER TABLE guilds ADD COLUMN welcome_msg TEXT NOT NULL DEFAULT '';

View File

@ -42,6 +42,10 @@ func vettingCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error {
return vettingRoleCmd(s, i)
case "req_channel":
return vettingReqChannelCmd(s, i)
case "welcome_channel":
return welcomeChannelCmd(s, i)
case "welcome_msg":
return welcomeMsgCmd(s, i)
default:
return fmt.Errorf("unknown vetting subcommand: %s", name)
}
@ -61,7 +65,7 @@ func vettingRoleCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error
return util.RespondEphemeral(s, i.Interaction, fmt.Sprintf("Successfully set %s as the vetting role!", role.Mention()))
}
// vettingReqChannelCmd sets the vettign request channel for a guild
// vettingReqChannelCmd sets the vetting request channel for a guild
func vettingReqChannelCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error {
data := i.ApplicationCommandData()
args := data.Options[0].Options
@ -75,6 +79,33 @@ func vettingReqChannelCmd(s *discordgo.Session, i *discordgo.InteractionCreate)
return util.RespondEphemeral(s, i.Interaction, fmt.Sprintf("Successfully set %s as the vetting request channel!", channel.Mention()))
}
// welcomeChannelCmd sets the welcome channel command for a guild
func welcomeChannelCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error {
data := i.ApplicationCommandData()
args := data.Options[0].Options
channel := args[0].ChannelValue(s)
err := db.SetWelcomeChannel(i.GuildID, channel.ID)
if err != nil {
return err
}
return util.RespondEphemeral(s, i.Interaction, fmt.Sprintf("Successfully set %s as the welcome channel!", channel.Mention()))
}
// welcomeChannelCmd sets the welcome message for a guild
func welcomeMsgCmd(s *discordgo.Session, i *discordgo.InteractionCreate) error {
data := i.ApplicationCommandData()
args := data.Options[0].Options
err := db.SetWelcomeMsg(i.GuildID, args[0].StringValue())
if err != nil {
return err
}
return util.RespondEphemeral(s, i.Interaction, "Successfully set the welcome message!")
}
// onMemberJoin adds the vetting role to a user when they join in order to allow them
// to access the vetting questions
func onMemberJoin(s *discordgo.Session, gma *discordgo.GuildMemberAdd) {
@ -84,7 +115,11 @@ func onMemberJoin(s *discordgo.Session, gma *discordgo.GuildMemberAdd) {
return
}
if guild.VettingRoleID == "" {
if guild.VettingRoleID == "" || guild.VettingReqChanID == "" {
err = welcomeUser(s, guild, gma.Member.User)
if err != nil {
log.Warn("Error welcoming user").Err(err)
}
return
}
@ -259,9 +294,23 @@ func onApprove(s *discordgo.Session, i *discordgo.InteractionCreate) error {
return err
}
err = welcomeUser(s, guild, user)
if err != nil {
return err
}
return util.RespondEphemeral(s, i.Interaction, "Successfully approved "+user.Mention()+" as "+role.Mention()+"!")
}
func welcomeUser(s *discordgo.Session, guild db.Guild, user *discordgo.User) error {
if guild.WelcomeChanID != "" && guild.WelcomeMsg != "" {
msg := strings.Replace(guild.WelcomeMsg, "$user", user.Mention(), 1)
_, err := s.ChannelMessageSend(guild.WelcomeChanID, msg)
return err
}
return nil
}
// onVettingResponse handles responses to vetting requests. If the user was accepted,
// it creates a vetting ticket for them. If they were rejected, it kicks them from the server.
func onVettingResponse(s *discordgo.Session, i *discordgo.InteractionCreate) error {

View File

@ -31,6 +31,10 @@ const (
)
func Init(s *discordgo.Session) error {
s.AddHandler(onMemberJoin)
s.AddHandler(util.InteractionErrorHandler("on-vetting-req", onVettingRequest))
s.AddHandler(util.InteractionErrorHandler("on-vetting-resp", onVettingResponse))
commands.Register(s, onMakeVettingMsg, &discordgo.ApplicationCommand{
Name: "Make Vetting Message",
Type: discordgo.MessageApplicationCommand,
@ -61,7 +65,7 @@ func Init(s *discordgo.Session) error {
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "role",
Name: "channel",
Description: "The channel to use for vetting requests",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
@ -69,6 +73,33 @@ func Init(s *discordgo.Session) error {
},
},
},
{
Name: "welcome_channel",
Description: "Set the welcome channel",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "channel",
Description: "The channel to use for welcoming new users",
Type: discordgo.ApplicationCommandOptionChannel,
ChannelTypes: []discordgo.ChannelType{discordgo.ChannelTypeGuildText},
Required: true,
},
},
},
{
Name: "welcome_msg",
Description: "Set the welcome message",
Type: discordgo.ApplicationCommandOptionSubCommand,
Options: []*discordgo.ApplicationCommandOption{
{
Name: "msg",
Description: "The message to welcome new users with",
Type: discordgo.ApplicationCommandOptionString,
Required: true,
},
},
},
},
})
@ -92,9 +123,5 @@ func Init(s *discordgo.Session) error {
},
})
s.AddHandler(onMemberJoin)
s.AddHandler(util.InteractionErrorHandler("on-vetting-req", onVettingRequest))
s.AddHandler(util.InteractionErrorHandler("on-vetting-resp", onVettingResponse))
return nil
}