Skip to content
Snippets Groups Projects
Commit c3499043 authored by Ophestra's avatar Ophestra
Browse files

simplify permission thingy on oauth URL, functions to iterate and get stuff,...

simplify permission thingy on oauth URL, functions to iterate and get stuff, automatic role assign on join
parent fcd9d741
Branches master
Tags v1.9.0
No related merge requests found
......@@ -60,7 +60,7 @@ func LateInitialize() error {
if err != nil {
return errors.New("unable to fetch application information")
}
state.InviteURL = fmt.Sprintf("https://discord.com/oauth2/authorize?client_id=%s&scope=bot&permissions=2146958847", state.Application.ID)
state.InviteURL = fmt.Sprintf("https://discord.com/oauth2/authorize?client_id=%s&scope=bot&permissions=8", state.Application.ID)
go func() {
for {
state.DiscordReady <- true
......
......@@ -26,20 +26,18 @@ func init() {
DatabaseKey: "greet_channel",
Cleanup: func(context *multiplexer.Context) {},
Validate: func(context *multiplexer.Context, input *string) (bool, bool) {
for _, channel := range context.Guild.Channels {
if *input == channel.ID {
if channel := context.GetChannel(*input); channel != nil {
*input = channel.ID
return true, true
}
}
} else {
return false, true
}
},
Format: func(context *multiplexer.Context, value string) (string, string, bool) {
for _, channel := range context.Guild.Channels {
if value == channel.ID {
if channel := context.GetChannel(value); channel != nil {
return channel.Name, channel.ID, true
}
}
return "No channel configured", fmt.Sprintf("Configure it by issuing command `%sconf greet channel <channelID>`.", context.Prefix()), true
return "No channel configured", fmt.Sprintf("Configure it by issuing command `%sconf greet channel <channel>`.", context.Prefix()), true
},
},
{
......
......@@ -51,21 +51,19 @@ func init() {
DatabaseKey: "highlight_channel",
Cleanup: func(context *multiplexer.Context) { config.ResetGuildMap(context.Guild.ID, "highlight") },
Validate: func(context *multiplexer.Context, input *string) (bool, bool) {
for _, channel := range context.Guild.Channels {
if *input == channel.ID {
if channel := context.GetChannel(*input); channel != nil {
*input = channel.ID
config.ResetGuildMap(context.Guild.ID, "highlight")
return true, true
}
}
} else {
return false, true
}
},
Format: func(context *multiplexer.Context, value string) (string, string, bool) {
for _, channel := range context.Guild.Channels {
if value == channel.ID {
if channel := context.GetChannel(value); channel != nil {
return channel.Name, channel.ID, true
}
}
return "No channel configured", fmt.Sprintf("Configure it by issuing command `%sconf highlight channel <channelID>`.", context.Prefix()), true
return "No channel configured", fmt.Sprintf("Configure it by issuing command `%sconf highlight channel <channel>`.", context.Prefix()), true
},
},
{
......
package internals
import (
"fmt"
"git.randomchars.net/RandomChars/FreeNitori/nitori/config"
"git.randomchars.net/RandomChars/FreeNitori/nitori/multiplexer"
"git.randomchars.net/RandomChars/FreeNitori/nitori/overrides"
"github.com/bwmarrin/discordgo"
)
func init() {
multiplexer.GuildMemberAdd = append(multiplexer.GuildMemberAdd, autoRoleHandler)
overrides.RegisterComplexEntry(overrides.ComplexConfigurationEntry{
Name: "role",
FriendlyName: "Role Assignment",
Description: "Configure role assignment related utilities.",
Entries: []overrides.SimpleConfigurationEntry{
{
Name: "join",
FriendlyName: "Automatic Role Assignment",
Description: "Role automatically assigned on join.",
DatabaseKey: "role_join",
Cleanup: func(context *multiplexer.Context) {},
Validate: func(context *multiplexer.Context, input *string) (bool, bool) {
if role := context.GetRole(*input); role != nil {
*input = role.ID
return true, true
} else {
return false, true
}
},
Format: func(context *multiplexer.Context, value string) (string, string, bool) {
if role := context.GetRole(value); role != nil {
return role.Name, role.ID, true
}
return "No role configured", fmt.Sprintf("Configure it by issuing command `%sconf role join <role>`.", context.Prefix()), true
},
},
},
CustomEntries: nil,
})
}
func autoRoleHandler(session *discordgo.Session, add *discordgo.GuildMemberAdd) {
role := false
roleID, err := config.GetGuildConfValue(add.GuildID, "role_join")
if err != nil {
return
}
if roleID == "" {
return
}
guild, err := session.State.Guild(add.GuildID)
if err != nil {
guild, err = session.Guild(add.GuildID)
if err != nil {
return
}
_ = session.State.GuildAdd(guild)
}
for _, r := range guild.Roles {
if r.ID == roleID {
role = true
}
}
if !role {
return
}
_ = session.GuildMemberRoleAdd(add.GuildID, add.User.ID, roleID)
}
......@@ -111,18 +111,18 @@ func (context *Context) IsAdministrator() bool {
}
// GetMember gets a member from a string representing it.
func (context *Context) GetMember(user string) *discordgo.Member {
func (context *Context) GetMember(query string) *discordgo.Member {
// Guild only function
if context.IsPrivate {
return nil
}
// Check if it's a mention or the string is numerical
_, err := strconv.Atoi(user)
if strings.HasPrefix(user, "<@") && strings.HasSuffix(user, ">") || err == nil {
_, err := strconv.Atoi(query)
if strings.HasPrefix(query, "<@") && strings.HasSuffix(query, ">") || err == nil {
// Strip off the mention thingy
userID := numericalRegex.ReplaceAllString(user, "")
// Length of a real user ID after stripping off stuff
userID := numericalRegex.ReplaceAllString(query, "")
// Length of a real snowflake after stripping off stuff
if len(userID) == 18 {
for _, member := range context.Guild.Members {
if member.User.ID == userID {
......@@ -133,7 +133,7 @@ func (context *Context) GetMember(user string) *discordgo.Member {
} else {
// Find as username or nickname
for _, member := range context.Guild.Members {
if member.User.Username == user || member.Nick == user {
if member.User.Username == query || member.Nick == query {
return member
}
}
......@@ -141,6 +141,68 @@ func (context *Context) GetMember(user string) *discordgo.Member {
return nil
}
// GetChannel gets a channel from a string representing it.
func (context *Context) GetChannel(query string) *discordgo.Channel {
// Guild only function
if context.IsPrivate {
return nil
}
// Check if it's a mention or the string is numerical
_, err := strconv.Atoi(query)
if strings.HasPrefix(query, "<#") && strings.HasSuffix(query, ">") || err == nil {
// Strip off the mention thingy
channelID := numericalRegex.ReplaceAllString(query, "")
// Length of a real snowflake after stripping off stuff
if len(channelID) == 18 {
for _, channel := range context.Guild.Channels {
if channel.ID == channelID {
return channel
}
}
}
} else {
// Find as channel name
for _, channel := range context.Guild.Channels {
if channel.Name == query {
return channel
}
}
}
return nil
}
// GetRole gets a channel from a string representing it.
func (context *Context) GetRole(query string) *discordgo.Role {
// Guild only function
if context.IsPrivate {
return nil
}
// Check if it's a mention or the string is numerical
_, err := strconv.Atoi(query)
if strings.HasPrefix(query, "<@&") && strings.HasSuffix(query, ">") || err == nil {
// Strip off the mention thingy
roleID := numericalRegex.ReplaceAllString(query, "")
// Length of a real snowflake after stripping off stuff
if len(roleID) == 18 {
for _, role := range context.Guild.Roles {
if role.ID == roleID {
return role
}
}
}
} else {
// Find as channel name
for _, role := range context.Guild.Roles {
if role.Name == query {
return role
}
}
}
return nil
}
// StitchFields stitches together fields of the message.
func (context *Context) StitchFields(start int) string {
message := context.Fields[start]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment