Skip to content

Commit

Permalink
Merge pull request #9 from Tobio89/feat/verify-command
Browse files Browse the repository at this point in the history
Add verify and deverify command
  • Loading branch information
Tobio89 authored Feb 19, 2023
2 parents 7689d4b + ff0deae commit 08893f3
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 9 deletions.
146 changes: 146 additions & 0 deletions app/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ func (c *Commands) create() {
allSuccessful = false
}

if _, err := c.bot.Session.ApplicationCommandCreate(c.bot.Cfg.bot.id, c.bot.Cfg.server.guild, VerifyCommand); err != nil {
c.bot.SendLog(msg.LogError, "Whilst adding verify command:")
c.bot.SendLog(msg.LogError, err.Error())
allSuccessful = false
}

if _, err := c.bot.Session.ApplicationCommandCreate(c.bot.Cfg.bot.id, c.bot.Cfg.server.guild, DeVerifyCommand); err != nil {
c.bot.SendLog(msg.LogError, "Whilst adding DeVerify command:")
c.bot.SendLog(msg.LogError, err.Error())
allSuccessful = false
}

if allSuccessful {
c.bot.SendLog(msg.LogOnReady, "All commands successfully added")
}
Expand Down Expand Up @@ -81,6 +93,7 @@ var EraseCommand = &discordgo.ApplicationCommand{
},
},
}

var StealthEraseCommand = &discordgo.ApplicationCommand{
Name: "erase-quietly",
Type: discordgo.ChatApplicationCommand,
Expand Down Expand Up @@ -133,6 +146,7 @@ var LearningResourceCommand = &discordgo.ApplicationCommand{
},
},
}

var RemindCommand = &discordgo.ApplicationCommand{
Name: "remind",
Type: discordgo.ChatApplicationCommand,
Expand All @@ -156,6 +170,33 @@ var RemindCommand = &discordgo.ApplicationCommand{
},
},
}
var VerifyCommand = &discordgo.ApplicationCommand{
Name: "verify",
Type: discordgo.ChatApplicationCommand,
Description: "Manually add verified role to user:",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "user",
Type: discordgo.ApplicationCommandOptionUser,
Description: "who to verify",
Required: true,
},
},
}

var DeVerifyCommand = &discordgo.ApplicationCommand{
Name: "deverify",
Type: discordgo.ChatApplicationCommand,
Description: "Manually remove verified role from user:",
Options: []*discordgo.ApplicationCommandOption{
{
Name: "user",
Type: discordgo.ApplicationCommandOptionUser,
Description: "who to de-verify",
Required: true,
},
},
}

func (c *Commands) AdminCommandGroup(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Type != discordgo.InteractionApplicationCommand {
Expand Down Expand Up @@ -282,6 +323,15 @@ func (c *Commands) AdminCommandGroup(s *discordgo.Session, i *discordgo.Interact

}

case "verify":
{
c.ManualVerify(i, options[0].UserValue(c.bot.Session))
}

case "deverify":
{
c.ManualDeVerify(i, options[0].UserValue(c.bot.Session))
}
}
}

Expand Down Expand Up @@ -474,3 +524,99 @@ func (c *Commands) DeleteMultipleMessages(eraseFromStartingPostID, eraseUntilPos

return nil
}

func (c *Commands) ManualVerify(i *discordgo.InteractionCreate, user *discordgo.User) {

var responseContent string
var userNick string = c.bot.Utils.GetUserNickOrUsername(user)

// Get member from server's member list
member, err := c.bot.Utils.GetMemberByID(user.ID)
if err != nil {
c.bot.SendLog(msg.LogError, "Whilst attempting manual verification:")
c.bot.SendLog(msg.LogError, err.Error())
responseContent = fmt.Sprintf("Failed to get member %s: have they left?", userNick)

c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: responseContent,
Flags: 1 << 6},
})
return
}
// Check if they have the role already
if hasRole, err := c.bot.Utils.MemberHasRoleByRoleID(member, c.bot.Cfg.roles.verified); err != nil || hasRole {
responseContent = fmt.Sprintf("User %s is already verified", userNick)
c.bot.SendLog(msg.CommandVerify, responseContent)

c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: responseContent,
Flags: 1 << 6},
})
return
}

c.bot.Session.GuildMemberRoleAdd(c.bot.Cfg.server.guild, member.User.ID, c.bot.Cfg.roles.verified)
c.bot.SendLog(msg.CommandVerify, fmt.Sprintf("User %s became verified manually", userNick))

if err := c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: fmt.Sprintf("User %s verified", userNick),
Flags: 1 << 6},
}); err != nil {
c.bot.SendLog(msg.LogError, "Whilst responding to command erase (single):")
c.bot.SendLog(msg.LogError, err.Error())
}
}

func (c *Commands) ManualDeVerify(i *discordgo.InteractionCreate, user *discordgo.User) {

var responseContent string
var userNick string = c.bot.Utils.GetUserNickOrUsername(user)

// Get member from server's member list
member, err := c.bot.Utils.GetMemberByID(user.ID)
if err != nil {
c.bot.SendLog(msg.LogError, "Whilst attempting manual de-verification:")
c.bot.SendLog(msg.LogError, err.Error())
responseContent = fmt.Sprintf("Failed to get member %s: have they left?", userNick)

c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: responseContent,
Flags: 1 << 6},
})
return
}
// Check if they have the role already
if hasRole, err := c.bot.Utils.MemberHasRoleByRoleID(member, c.bot.Cfg.roles.verified); err != nil || !hasRole {
responseContent = fmt.Sprintf("User %s is already not verified", userNick)
c.bot.SendLog(msg.CommandDeVerify, fmt.Sprintf("User %s is already not verified", userNick))

c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: responseContent,
Flags: 1 << 6},
})
return
}

c.bot.Session.GuildMemberRoleRemove(c.bot.Cfg.server.guild, member.User.ID, c.bot.Cfg.roles.verified)
c.bot.SendLog(msg.CommandDeVerify, fmt.Sprintf("User %s became de-verified", userNick))

if err := c.bot.Session.InteractionRespond(i.Interaction,
&discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{Content: fmt.Sprintf("User %s de-verified", userNick),
Flags: 1 << 6},
}); err != nil {
c.bot.SendLog(msg.LogError, "Whilst responding to command de-verify:")
c.bot.SendLog(msg.LogError, err.Error())
}
}
2 changes: 1 addition & 1 deletion app/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func (e *Events) handleIntroductionVerification(m *discordgo.MessageCreate) {
return
}

if hasRole, err := e.bot.Utils.UserHasRoleByRoleID(member, e.bot.Cfg.roles.verified); err != nil || hasRole {
if hasRole, err := e.bot.Utils.MemberHasRoleByRoleID(member, e.bot.Cfg.roles.verified); err != nil || hasRole {
return
}

Expand Down
4 changes: 3 additions & 1 deletion app/msg/greetings.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ const (
const (
CommandErase string = "<Erase> "
CommandForceLog string = "<Force Log> "
CommandRemind string = "<Remind > "
CommandRemind string = "<Remind> "
CommandVerify string = "<Verify> "
CommandDeVerify string = "<DeVerify> "
)
20 changes: 15 additions & 5 deletions app/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (u *Utils) UserHasRoleByRoleName(member *discordgo.Member, roleToFind strin
}

// Return boolean: does user have role, from role's ID string
func (u *Utils) UserHasRoleByRoleID(member *discordgo.Member, roleToFind string) (bool, error) {
func (u *Utils) MemberHasRoleByRoleID(member *discordgo.Member, roleToFind string) (bool, error) {

for _, userExistingRoleID := range member.Roles {
if userExistingRoleID == roleToFind {
Expand Down Expand Up @@ -128,9 +128,19 @@ func (u *Utils) IsUserAdmin(userID string) (bool, error) {
return u.MemberHasPermission(userID, discordgo.PermissionAdministrator)
}

func (u *Utils) GetMemberNickOrUsername(user discordgo.Member) string {
if user.Nick != "" {
return user.Nick
func (u *Utils) GetMemberNickOrUsername(member discordgo.Member) string {
if member.Nick != "" {
return member.Nick
}
return user.User.Username
return member.User.Username
}

func (u *Utils) GetUserNickOrUsername(user *discordgo.User) string {

member, _ := u.GetMemberByID(user.ID)

if member.Nick != "" {
return member.Nick
}
return member.User.Username
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE=
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/bwmarrin/discordgo v0.27.0 h1:4ZK9KN+rGIxZ0fdGTmgdCcliQeW8Zhu6MnlFI92nf0Q=
github.com/bwmarrin/discordgo v0.27.0/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
Expand Down

0 comments on commit 08893f3

Please sign in to comment.