first implementation
This commit is contained in:
167
main.go
167
main.go
@@ -4,11 +4,49 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
"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() {
|
func main() {
|
||||||
//discordServerId := os.Getenv("DISCORD_SERVER_ID")
|
//discordServerId := os.Getenv("DISCORD_SERVER_ID")
|
||||||
//discordChannelId := os.Getenv("DISCORD_CHANNEL_ID")
|
//discordChannelId := os.Getenv("DISCORD_CHANNEL_ID")
|
||||||
@@ -25,29 +63,96 @@ func main() {
|
|||||||
})
|
})
|
||||||
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
discord.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
if i.Type == discordgo.InteractionApplicationCommand {
|
if i.Type == discordgo.InteractionApplicationCommand {
|
||||||
|
roles, _ := s.GuildRoles(i.GuildID)
|
||||||
|
var discordWaintingRoleId string
|
||||||
|
for _, role := range roles {
|
||||||
|
if role.Name == "WAITING" {
|
||||||
|
discordWaintingRoleId = role.ID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fmt.Println("GuildId: ", i.GuildID, " User ID: ", i.Member.User.ID, " Role ID: ", discordWaintingRoleId)
|
||||||
switch i.ApplicationCommandData().Name {
|
switch i.ApplicationCommandData().Name {
|
||||||
case "waitlistadd":
|
case "waitlistadd":
|
||||||
// Handle addwait command
|
// Handle addwait command
|
||||||
roles, _ := s.GuildRoles(i.GuildID)
|
// check if user is already in the waiting list
|
||||||
var discordWaintingRoleId string
|
index := lookupWaitingUserIndex(i.Member.User.ID)
|
||||||
for _, role := range roles {
|
if index == -1 { //not in waiting list
|
||||||
if role.Name == "WAITING" {
|
err2 := discord.GuildMemberRoleAdd(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
||||||
discordWaintingRoleId = role.ID
|
if err2 != nil {
|
||||||
|
panic(err2)
|
||||||
}
|
}
|
||||||
}
|
timer := time.AfterFunc(WaitingLength, func() {
|
||||||
fmt.Println("GuildId: ", i.GuildID, " User ID: ", i.Member.User.ID, " Role ID: ", discordWaintingRoleId)
|
discord.GuildMemberRoleRemove(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
||||||
err2 := discord.GuildMemberRoleAdd(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
index := lookupWaitingUserIndex(i.Member.User.ID)
|
||||||
if err2 != nil {
|
if index != -1 {
|
||||||
panic(err2)
|
WaitingUsersList = append(WaitingUsersList[:index], WaitingUsersList[index+1:]...)
|
||||||
}
|
}
|
||||||
time.AfterFunc(1*time.Minute, func() {
|
})
|
||||||
discord.GuildMemberRoleRemove(i.GuildID, i.Member.User.ID, discordWaintingRoleId)
|
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{
|
s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: "User " + i.Member.User.Username + " added to waiting list. ",
|
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{},
|
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)
|
stop := make(chan os.Signal, 1)
|
||||||
signal.Notify(stop, os.Interrupt)
|
signal.Notify(stop, os.Interrupt)
|
||||||
fmt.Println("Press Ctrl+C to exit")
|
fmt.Println("Press Ctrl+C to exit")
|
||||||
<-stop
|
<-stop
|
||||||
// add role to user
|
|
||||||
/*
|
|
||||||
err2 := discord.GuildMemberRoleAdd(discordServerId, "316091695941025793", discordWaintingRoleId)
|
|
||||||
if err2 != nil {
|
|
||||||
panic(err2)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user