Compare commits
4 Commits
3abc4f3265
...
ImplDiscor
Author | SHA1 | Date | |
---|---|---|---|
78e8a857e9
|
|||
849f592fc0
|
|||
d7c884bf1d
|
|||
71bec93b58
|
@@ -1,6 +1,8 @@
|
|||||||
[discord]
|
[discord]
|
||||||
client = ""
|
token = ""
|
||||||
|
appid = ""
|
||||||
guildid = ""
|
guildid = ""
|
||||||
|
category_id = ""
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
host = ""
|
host = ""
|
||||||
@@ -9,3 +11,4 @@ name = ""
|
|||||||
username = ""
|
username = ""
|
||||||
password = ""
|
password = ""
|
||||||
|
|
||||||
|
admin_roles = [""]
|
||||||
|
213
main.go
213
main.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/BurntSushi/toml"
|
"github.com/BurntSushi/toml"
|
||||||
@@ -17,19 +18,19 @@ var (
|
|||||||
config Config
|
config Config
|
||||||
client *discordgo.Session
|
client *discordgo.Session
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
|
|
||||||
globalCategoryID string
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Discord Discord `toml:"discord"`
|
Discord Discord `toml:"discord"`
|
||||||
Database Database `toml:"database"`
|
Database Database `toml:"database"`
|
||||||
|
AdminRoles []string `toml:"admin_roles"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Discord struct {
|
type Discord struct {
|
||||||
Token string `toml:"token"`
|
Token string `toml:"token"`
|
||||||
AppID string `toml:"appid"`
|
AppID string `toml:"appid"`
|
||||||
GuildID string `toml:"guildid"`
|
GuildID string `toml:"guildid"`
|
||||||
|
CategoryID string `toml:"category_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Database struct {
|
type Database struct {
|
||||||
@@ -92,6 +93,27 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func reset(userNickname, softwareType string) {
|
||||||
|
log.Printf("Resetting %s for user %s\n", softwareType, userNickname)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUsernameFromMember(member *discordgo.Member) string {
|
||||||
|
if member == nil {
|
||||||
|
return "UnknownUser"
|
||||||
|
}
|
||||||
|
|
||||||
|
var userName string
|
||||||
|
if member.Nick != "" {
|
||||||
|
userName = member.Nick
|
||||||
|
} else if member.User != nil && member.User.Username != "" {
|
||||||
|
userName = member.User.Username
|
||||||
|
} else {
|
||||||
|
userName = "UnknownUser"
|
||||||
|
}
|
||||||
|
return userName
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
commands = []discordgo.ApplicationCommand{
|
commands = []discordgo.ApplicationCommand{
|
||||||
{
|
{
|
||||||
@@ -104,23 +126,12 @@ var (
|
|||||||
Description: "Channel for ticket creation",
|
Description: "Channel for ticket creation",
|
||||||
Required: true,
|
Required: true,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
Type: discordgo.ApplicationCommandOptionChannel,
|
|
||||||
Name: "category",
|
|
||||||
Description: "Category for tickets",
|
|
||||||
Required: true,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
commandsHandlers = map[string]func(client *discordgo.Session, i *discordgo.InteractionCreate){
|
||||||
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
"setup": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client)
|
channelOption := i.ApplicationCommandData().Options[0].ChannelValue(client)
|
||||||
categoryOption := i.ApplicationCommandData().Options[1].ChannelValue(client)
|
|
||||||
|
|
||||||
globalCategoryID = categoryOption.ID
|
|
||||||
|
|
||||||
|
|
||||||
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
|
_, err := client.ChannelMessageSendComplex(channelOption.ID, &discordgo.MessageSend{
|
||||||
Content: "Click the button below to request a HWID reset:",
|
Content: "Click the button below to request a HWID reset:",
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
@@ -139,7 +150,6 @@ var (
|
|||||||
log.Println("Error sending message to the specified channel:", err)
|
log.Println("Error sending message to the specified channel:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
@@ -156,7 +166,7 @@ var (
|
|||||||
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: "For what would u like a HWID reset:",
|
Content: "For what would you like a HWID reset:",
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
discordgo.ActionsRow{
|
discordgo.ActionsRow{
|
||||||
Components: []discordgo.MessageComponent{
|
Components: []discordgo.MessageComponent{
|
||||||
@@ -195,26 +205,35 @@ var (
|
|||||||
softwareType = "Mesa"
|
softwareType = "Mesa"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
categoryID := config.Discord.CategoryID
|
||||||
guildID := i.GuildID
|
guildID := i.GuildID
|
||||||
userID := i.Member.User.ID
|
userID := i.Member.User.ID
|
||||||
userName := i.Member.User.Username
|
userName := getUsernameFromMember(i.Member)
|
||||||
|
|
||||||
|
if !canCreateTicket(userName, selectedOption) {
|
||||||
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: fmt.Sprintf("You already have an active %s ticket. Please wait for the administrators to process it.", softwareType),
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending interaction response:", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
|
channel, err := client.GuildChannelCreateComplex(guildID, discordgo.GuildChannelCreateData{
|
||||||
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
|
Name: fmt.Sprintf("reset-%s-%s", softwareType, userName),
|
||||||
Type: discordgo.ChannelTypeGuildText,
|
Type: discordgo.ChannelTypeGuildText,
|
||||||
ParentID: globalCategoryID,
|
ParentID: categoryID,
|
||||||
PermissionOverwrites: []*discordgo.PermissionOverwrite{
|
PermissionOverwrites: []*discordgo.PermissionOverwrite{
|
||||||
{
|
{
|
||||||
ID: guildID,
|
ID: guildID,
|
||||||
Type: discordgo.PermissionOverwriteTypeRole,
|
Type: discordgo.PermissionOverwriteTypeRole,
|
||||||
Deny: discordgo.PermissionViewChannel,
|
Deny: discordgo.PermissionViewChannel,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
ID: userID,
|
|
||||||
Type: discordgo.PermissionOverwriteTypeMember,
|
|
||||||
Allow: discordgo.PermissionViewChannel,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -222,20 +241,151 @@ var (
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, roleID := range config.AdminRoles {
|
||||||
|
client.ChannelPermissionSet(channel.ID, roleID, discordgo.PermissionOverwriteTypeRole, discordgo.PermissionViewChannel, 0)
|
||||||
|
}
|
||||||
|
|
||||||
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
Data: &discordgo.InteractionResponseData{
|
Data: &discordgo.InteractionResponseData{
|
||||||
Content: fmt.Sprintf("Request created: %s", channel.Mention()),
|
Content: "Request submitted, you'll get a PM when it has been processed",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending interaction response:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.ChannelMessageSendComplex(channel.ID, &discordgo.MessageSend{
|
||||||
|
Content: fmt.Sprintf("Reset request by %s for %s", userName, softwareType),
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.ActionsRow{
|
||||||
|
Components: []discordgo.MessageComponent{
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Accept",
|
||||||
|
Style: discordgo.PrimaryButton,
|
||||||
|
CustomID: fmt.Sprintf("accept_%s_%s", userID, softwareType),
|
||||||
|
},
|
||||||
|
discordgo.Button{
|
||||||
|
Label: "Decline",
|
||||||
|
Style: discordgo.DangerButton,
|
||||||
|
CustomID: fmt.Sprintf("decline_%s_%s", userID, softwareType),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending message to the ticket channel:", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"accept": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
data := i.MessageComponentData().CustomID
|
||||||
|
parts := strings.Split(data, "_")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
log.Println("Invalid accept button custom ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID := parts[1]
|
||||||
|
softwareType := parts[2]
|
||||||
|
|
||||||
|
member, err := client.GuildMember(i.GuildID, userID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error fetching member info:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userName := getUsernameFromMember(member)
|
||||||
|
|
||||||
|
reset(userName, softwareType)
|
||||||
|
|
||||||
|
err = client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Request accepted and processed.",
|
||||||
Flags: discordgo.MessageFlagsEphemeral,
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error sending interaction response:", err)
|
log.Println("Error sending interaction response:", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_, err = client.ChannelDelete(i.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error deleting channel:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dmChannel, err := client.UserChannelCreate(userID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error creating DM channel:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.ChannelMessageSend(dmChannel.ID, fmt.Sprintf("Your reset request for %s has been accepted and processed.", softwareType))
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending DM:", err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
"decline": func(client *discordgo.Session, i *discordgo.InteractionCreate) {
|
||||||
|
data := i.MessageComponentData().CustomID
|
||||||
|
parts := strings.Split(data, "_")
|
||||||
|
if len(parts) != 3 {
|
||||||
|
log.Println("Invalid decline button custom ID")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
userID := parts[1]
|
||||||
|
|
||||||
|
err := client.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
|
||||||
|
Type: discordgo.InteractionResponseChannelMessageWithSource,
|
||||||
|
Data: &discordgo.InteractionResponseData{
|
||||||
|
Content: "Request declined.",
|
||||||
|
Flags: discordgo.MessageFlagsEphemeral,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending interaction response:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.ChannelDelete(i.ChannelID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error deleting channel:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
dmChannel, err := client.UserChannelCreate(userID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error creating DM channel:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = client.ChannelMessageSend(dmChannel.ID, "Your reset request has been declined.")
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error sending DM:", err)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func canCreateTicket(userName, softwareType string) bool {
|
||||||
|
guildChannels, err := client.GuildChannels(config.Discord.GuildID)
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error fetching guild channels:", err)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, channel := range guildChannels {
|
||||||
|
if channel.Type == discordgo.ChannelTypeGuildText && channel.ParentID == config.Discord.CategoryID {
|
||||||
|
expectedName := fmt.Sprintf("reset-%s-%s", softwareType, userName)
|
||||||
|
if channel.Name == expectedName {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if client == nil {
|
if client == nil {
|
||||||
log.Println("Bot client is not initialized")
|
log.Println("Bot client is not initialized")
|
||||||
@@ -263,8 +413,13 @@ func main() {
|
|||||||
h(client, i)
|
h(client, i)
|
||||||
}
|
}
|
||||||
} else if i.Type == discordgo.InteractionMessageComponent {
|
} else if i.Type == discordgo.InteractionMessageComponent {
|
||||||
if h, ok := componentsHandlers[i.MessageComponentData().CustomID]; ok {
|
customID := i.MessageComponentData().CustomID
|
||||||
|
if h, ok := componentsHandlers[customID]; ok {
|
||||||
h(client, i)
|
h(client, i)
|
||||||
|
} else if strings.HasPrefix(customID, "accept_") {
|
||||||
|
componentsHandlers["accept"](client, i)
|
||||||
|
} else if strings.HasPrefix(customID, "decline_") {
|
||||||
|
componentsHandlers["decline"](client, i)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user