Skip to content

Commit

Permalink
added ban stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Jul 22, 2021
1 parent 84db0a8 commit 7c0c3f4
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 3 deletions.
13 changes: 13 additions & 0 deletions api/ban.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package api

// Ban represents a banned User from a Guild (https://discord.com/developers/docs/resources/guild#ban-object)
type Ban struct {
Reason *string `json:"reason,omitempty"`
User *User `json:"user"`
}

// AddBan is used to ban a User (https://discord.com/developers/docs/resources/guild#create-guild-ban-json-params)
type AddBan struct {
DeleteMessageDays int `json:"delete_message_days,omitempty"`
Reason string `json:"reason,omitempty"`
}
20 changes: 20 additions & 0 deletions api/guild.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,26 @@ func (g *Guild) IconURL(size int) *string {
return &u
}

// GetBans fetches all bans for this Guild
func (g *Guild) GetBans() ([]Ban, restclient.RestError) {
return g.Disgo.RestClient().GetBans(g.ID)
}

// GetBan fetches a ban for a User for this Guild
func (g *Guild) GetBan(userID Snowflake) (*Ban, restclient.RestError) {
return g.Disgo.RestClient().GetBan(g.ID, userID)
}

// BanMember bans a Member from the Guild
func (g *Guild) BanMember(userID Snowflake, reason string, deleteMessageDays int) restclient.RestError {
return g.Disgo.RestClient().AddBan(g.ID, userID, reason, deleteMessageDays)
}

// UnbanMember unbans a Member from the Guild
func (g *Guild) UnbanMember(userID Snowflake) restclient.RestError {
return g.Disgo.RestClient().DeleteBan(g.ID, userID)
}

// GetCommand fetches a specific Guild Command
func (g *Guild) GetCommand(commandID Snowflake) (*Command, restclient.RestError) {
return g.Disgo.GetGuildCommand(g.ID, commandID)
Expand Down
10 changes: 10 additions & 0 deletions api/member.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func (m *Member) Kick(reason string) restclient.RestError {
return m.Disgo.RestClient().RemoveMember(m.GuildID, m.User.ID, reason)
}

// Ban bans the Member from the Guild
func (m *Member) Ban(reason string, deleteMessageDays int) restclient.RestError {
return m.Disgo.RestClient().AddBan(m.GuildID, m.User.ID, reason, deleteMessageDays)
}

// Unban unbans the Member from the Guild
func (m *Member) Unban() restclient.RestError {
return m.Disgo.RestClient().DeleteBan(m.GuildID, m.User.ID)
}

// Move moves/kicks the member to/from a voice channel
func (m *Member) Move(channelID *Snowflake) (*Member, restclient.RestError) {
return m.Disgo.RestClient().MoveMember(m.GuildID, m.User.ID, channelID)
Expand Down
5 changes: 5 additions & 0 deletions api/restclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ type RestClient interface {
AddMemberRole(guildID Snowflake, userID Snowflake, roleID Snowflake) restclient.RestError
RemoveMemberRole(guildID Snowflake, userID Snowflake, roleID Snowflake) restclient.RestError

GetBans(guildID Snowflake) ([]Ban, restclient.RestError)
GetBan(guildID Snowflake, userID Snowflake) (*Ban, restclient.RestError)
AddBan(guildID Snowflake, userID Snowflake, reason string, deleteMessageDays int) restclient.RestError
DeleteBan(guildID Snowflake, userID Snowflake) restclient.RestError

GetRoles(guildID Snowflake) ([]*Role, restclient.RestError)
CreateRole(guildID Snowflake, createRole CreateRole) (*Role, restclient.RestError)
UpdateRole(guildID Snowflake, roleID Snowflake, updateRole UpdateRole) (*Role, restclient.RestError)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/DisgoOrg/log v1.0.3
github.com/DisgoOrg/restclient v1.2.2
github.com/DisgoOrg/restclient v1.2.3
github.com/gorilla/mux v1.8.0
github.com/gorilla/websocket v1.4.2
github.com/stretchr/testify v1.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/DisgoOrg/log v1.0.3 h1:IjmZQQu/kuBIui22EdXmxzQGYwcPCJEkXa0Fe6W9fJk=
github.com/DisgoOrg/log v1.0.3/go.mod h1:KFGKhBQr37d6rxZ7p2bmc8BEmDH8DZbtgdlJDSCsE7I=
github.com/DisgoOrg/restclient v1.2.2 h1:75SGqqN/9lqJ3GQOpPY99f1ihZMzSXDcLNF+biDp+Pk=
github.com/DisgoOrg/restclient v1.2.2/go.mod h1:PIhyYsT52w5T6m4LT+HTdKqY6NOIqo71Ai0rgaq9ZtM=
github.com/DisgoOrg/restclient v1.2.3 h1:lSvsS63oYlnUH72dgIVfc+hWS24rPzHyaf22ZPF0E7Q=
github.com/DisgoOrg/restclient v1.2.3/go.mod h1:PIhyYsT52w5T6m4LT+HTdKqY6NOIqo71Ai0rgaq9ZtM=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
Expand Down
40 changes: 40 additions & 0 deletions internal/restclient_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,46 @@ func (r *restClientImpl) MoveMember(guildID api.Snowflake, userID api.Snowflake,
return
}

func (r *restClientImpl) GetBans(guildID api.Snowflake) (bans []api.Ban, rErr restclient.RestError) {
compiledRoute, err := restclient.GetBans.Compile(nil, guildID)
if err != nil {
return nil, restclient.NewError(nil, err)
}
rErr = r.Do(compiledRoute, nil, &bans)
if rErr == nil {
for _, ban := range bans {
ban.User = r.Disgo().EntityBuilder().CreateUser(ban.User, api.CacheStrategyNoWs)
}
}
return
}
func (r *restClientImpl) GetBan(guildID api.Snowflake, userID api.Snowflake) (ban *api.Ban, rErr restclient.RestError) {
compiledRoute, err := restclient.GetBan.Compile(nil, guildID, userID)
if err != nil {
return nil, restclient.NewError(nil, err)
}
rErr = r.Do(compiledRoute, nil, &ban)
if rErr == nil {
ban.User = r.Disgo().EntityBuilder().CreateUser(ban.User, api.CacheStrategyNoWs)
}
return
}
func (r *restClientImpl) AddBan(guildID api.Snowflake, userID api.Snowflake, reason string, deleteMessageDays int) restclient.RestError {
compiledRoute, err := restclient.AddBan.Compile(nil, guildID, userID)
if err != nil {
return restclient.NewError(nil, err)
}
return r.Do(compiledRoute, api.AddBan{DeleteMessageDays: deleteMessageDays, Reason: reason}, nil)
}

func (r *restClientImpl) DeleteBan(guildID api.Snowflake, userID api.Snowflake) restclient.RestError {
compiledRoute, err := restclient.DeleteBan.Compile(nil, guildID, userID)
if err != nil {
return restclient.NewError(nil, err)
}
return r.Do(compiledRoute, nil, nil)
}

// AddMemberRole adds a api.Role to a api.Member
func (r *restClientImpl) AddMemberRole(guildID api.Snowflake, userID api.Snowflake, roleID api.Snowflake) (rErr restclient.RestError) {
compiledRoute, err := restclient.AddMemberRole.Compile(nil, guildID, userID, roleID)
Expand Down

0 comments on commit 7c0c3f4

Please sign in to comment.