This repository has been archived by the owner on Apr 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
45cce77
commit 59907e0
Showing
11 changed files
with
121 additions
and
130 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package lastread | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/diamondburned/arikawa/discord" | ||
"github.com/diamondburned/gtkcord3/gtkcord/config" | ||
"github.com/diamondburned/gtkcord3/internal/log" | ||
) | ||
|
||
type State struct { | ||
path string | ||
store *store | ||
} | ||
|
||
type store struct { | ||
sync.Mutex `json:"-"` | ||
|
||
// GuildID -> ChannelID; if GuildID == 0 then DM | ||
Access map[discord.Snowflake]discord.Snowflake | ||
} | ||
|
||
func New(file string) State { | ||
store := &store{ | ||
Access: make(map[discord.Snowflake]discord.Snowflake), | ||
} | ||
|
||
if err := store.load(file); err != nil { | ||
log.Errorln("Failed to load config:", err) | ||
} | ||
|
||
return State{ | ||
path: file, | ||
store: store, | ||
} | ||
} | ||
|
||
func (store *store) load(file string) error { | ||
store.Lock() | ||
defer store.Unlock() | ||
|
||
return config.UnmarshalFromFile(file, store) | ||
} | ||
|
||
func (store *store) save(file string) error { | ||
store.Lock() | ||
defer store.Unlock() | ||
|
||
return config.MarshalToFile(file, store) | ||
} | ||
|
||
func (s *State) Access(guild discord.Snowflake) discord.Snowflake { | ||
s.store.Lock() | ||
defer s.store.Unlock() | ||
|
||
id, _ := s.store.Access[guild] | ||
return id | ||
} | ||
|
||
func (s *State) SetAccess(guild, channel discord.Snowflake) { | ||
s.store.Lock() | ||
s.store.Access[guild] = channel | ||
s.store.Unlock() | ||
|
||
if err := s.store.save(s.path); err != nil { | ||
log.Errorln("Failed to save config:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.