first implementation
This commit is contained in:
147
main.go
147
main.go
@@ -4,11 +4,49 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
var WaitingLength = 1 * time.Minute
|
||||
|
||||
// user data
|
||||
type WaitingUser struct {
|
||||
ID string
|
||||
Username string
|
||||
AddedAt time.Time
|
||||
EndAt time.Time
|
||||
Timer *time.Timer
|
||||
}
|
||||
|
||||
func NewWaitingUser(id, username string, timer *time.Timer) WaitingUser {
|
||||
return WaitingUser{
|
||||
ID: id,
|
||||
Username: username,
|
||||
AddedAt: time.Now(),
|
||||
EndAt: time.Now().Add(WaitingLength),
|
||||
Timer: timer,
|
||||
}
|
||||
}
|
||||
|
||||
func (wu *WaitingUser) UpdateTimer() {
|
||||
wu.Timer.Reset(WaitingLength)
|
||||
wu.EndAt = time.Now().Add(WaitingLength)
|
||||
}
|
||||
|
||||
var WaitingUsersList []WaitingUser
|
||||
|
||||
func lookupWaitingUserIndex(userID string) int {
|
||||
for i := range WaitingUsersList {
|
||||
if WaitingUsersList[i].ID == userID {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func main() {
|
||||
//discordServerId := os.Getenv("DISCORD_SERVER_ID")
|
||||
//discordChannelId := os.Getenv("DISCORD_CHANNEL_ID")
|
||||
@@ -25,9 +63,6 @@ func main() {
|
||||
})
|
||||
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||
if i.Type == discordgo.InteractionApplicationCommand {
|
||||
switch i.ApplicationCommandData().Name {
|
||||
case "waitlistadd":
|
||||
// Handle addwait command
|
||||
roles, _ := s.GuildRoles(i.GuildID)
|
||||
var discordWaintingRoleId string
|
||||
for _, role := range roles {
|
||||
@@ -36,20 +71,90 @@ func main() {
|
||||
}
|
||||
}
|
||||
fmt.Println("GuildId: ", i.GuildID, " User ID: ", i.Member.User.ID, " Role ID: ", discordWaintingRoleId)
|
||||
switch i.ApplicationCommandData().Name {
|
||||
case "waitlistadd":
|
||||
// Handle addwait command
|
||||
// check if user is already in the waiting list
|
||||
index := lookupWaitingUserIndex(i.Member.User.ID)
|
||||
if index == -1 { //not in waiting list
|
||||
err2 := discord.GuildMemberRoleAdd(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
||||
if err2 != nil {
|
||||
panic(err2)
|
||||
}
|
||||
time.AfterFunc(1*time.Minute, func() {
|
||||
timer := time.AfterFunc(WaitingLength, func() {
|
||||
discord.GuildMemberRoleRemove(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
||||
index := lookupWaitingUserIndex(i.Member.User.ID)
|
||||
if index != -1 {
|
||||
WaitingUsersList = append(WaitingUsersList[:index], WaitingUsersList[index+1:]...)
|
||||
}
|
||||
})
|
||||
|
||||
WaitingUsersList = append(WaitingUsersList, NewWaitingUser(i.Member.User.ID, i.Member.User.Username, timer))
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "User " + i.Member.User.Username + " added to waiting list. ",
|
||||
},
|
||||
})
|
||||
|
||||
} else {
|
||||
// User is already in the waiting list, extend their waiting time
|
||||
WaitingUsersList[index].UpdateTimer()
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "User " + i.Member.User.Username + " is already in the waiting list and extend waiting time.",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
case "waitlistshow":
|
||||
// Handle waitlistshow command
|
||||
var waitingUsersStr []string
|
||||
for _, user := range WaitingUsersList {
|
||||
waitingUsersStr = append(waitingUsersStr, user.Username)
|
||||
}
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "Waiting list: " + strings.Join(waitingUsersStr, ", "),
|
||||
},
|
||||
})
|
||||
case "waitlistremove":
|
||||
// Handle waitlistremove command
|
||||
userID := i.ApplicationCommandData().GetOption("user").UserValue(s).ID
|
||||
userName := i.ApplicationCommandData().GetOption("user").UserValue(s).Username
|
||||
discord.GuildMemberRoleRemove(i.GuildID, userID, discordWaintingRoleId)
|
||||
index := lookupWaitingUserIndex(userID)
|
||||
if index != -1 {
|
||||
WaitingUsersList = append(WaitingUsersList[:index], WaitingUsersList[index+1:]...)
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "User " + userName + " removed from waiting list.",
|
||||
},
|
||||
})
|
||||
} else {
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "User " + userName + " is not in the waiting list.",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
case "waitlistremoveall":
|
||||
// Handle waitlistremoveall command
|
||||
for _, user := range WaitingUsersList {
|
||||
discord.GuildMemberRoleRemove(i.GuildID, user.ID, discordWaintingRoleId)
|
||||
user.Timer.Stop()
|
||||
}
|
||||
WaitingUsersList = nil
|
||||
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||
Data: &discordgo.InteractionResponseData{
|
||||
Content: "All users removed from waiting list.",
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -65,16 +170,34 @@ func main() {
|
||||
Options: []*discordgo.ApplicationCommandOption{},
|
||||
})
|
||||
|
||||
discord.ApplicationCommandCreate(discord.State.User.ID, "", &discordgo.ApplicationCommand{
|
||||
Name: "waitlistshow",
|
||||
Description: "Show the waiting list",
|
||||
Options: []*discordgo.ApplicationCommandOption{},
|
||||
})
|
||||
|
||||
discord.ApplicationCommandCreate(discord.State.User.ID, "", &discordgo.ApplicationCommand{
|
||||
Name: "waitlistremove",
|
||||
Description: "Remove a user from the waiting list",
|
||||
Options: []*discordgo.ApplicationCommandOption{
|
||||
{
|
||||
Name: "user",
|
||||
Description: "The user to remove from the waiting list",
|
||||
Type: discordgo.ApplicationCommandOptionUser,
|
||||
Required: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
discord.ApplicationCommandCreate(discord.State.User.ID, "", &discordgo.ApplicationCommand{
|
||||
Name: "waitlistremoveall",
|
||||
Description: "Remove all users from the waiting list",
|
||||
Options: []*discordgo.ApplicationCommandOption{},
|
||||
})
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, os.Interrupt)
|
||||
fmt.Println("Press Ctrl+C to exit")
|
||||
<-stop
|
||||
// add role to user
|
||||
/*
|
||||
err2 := discord.GuildMemberRoleAdd(discordServerId, "316091695941025793", discordWaintingRoleId)
|
||||
if err2 != nil {
|
||||
panic(err2)
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user