go-ircevent icon indicating copy to clipboard operation
go-ircevent copied to clipboard

Getting user perms?

Open Dontmindmes opened this issue 6 years ago • 4 comments

How can i get the senders perms?

ex: sender uses !test -> checks if sender has op perms

Dontmindmes avatar Apr 23 '19 19:04 Dontmindmes

This is actually fairly complicated and involves setting up a number of callbacks and tracking a bunch of stuff.

I've got a relatively detailed write up of the callbacks you'll need here: https://gist.github.com/belak/09edcc4f5e51056bf5bc728647659d81

belak avatar Apr 23 '19 20:04 belak

ugh its to confusing :/

if wish there was somthing like e.Perm("Username") that returns true or false

Dontmindmes avatar Apr 24 '19 03:04 Dontmindmes

Unfortunately because of how IRC is designed (there's not consistency between daemons) this is hard. There are some IRCv3 CAPs you could use (which I think are mentioned in the gist) but they don't work everywhere.

belak avatar Apr 24 '19 04:04 belak

I;ve been messing a bit with this, and I have come up with a helper function that illustrates how to do it. I use this to check if the bot has +o:

// RequestReply abstracts sending a command to the IRC server and listening for a reply. Eventcodes must match the
// commands. See https://www.alien.net.au/irc/irc2numerics.html
func RequestReply(c *irc.Connection, eventcode, command string) (string, error) {
	reply := make(chan string, 1)
	id := c.AddCallback(eventcode, func(e *irc.Event) {
		reply <- e.Message()
		defer close(reply)
	})
	defer c.RemoveCallback(eventcode, id)
	c.SendRaw(command)
	var r string
	select {
	case r = <-reply:
	case <-time.NewTicker(5 * time.Second).C:
		return "", errors.New("timeout getting command response")
	}
	return r, nil
}

Then I can use it to send a "NAMES -ops " command to the IRC server, and record the response:

o, err := RequestReply(c, "366", "NAMES -ops "+channel)
if err != nil {
	SendReply(c, channel, fmt.Sprintf("Error getting ops list: %s", err), false)
	return
}

o is then a space-separated string of all channel operators on channel. Then you just need to make sure your bot's name's in there. However, see also #130

adamhassel avatar Apr 07 '20 18:04 adamhassel