start on Channel class

This commit is contained in:
Cadey Ratio 2019-12-26 03:59:30 +00:00
parent ac061dff7d
commit fa56644a7e
2 changed files with 70 additions and 0 deletions

View File

@ -0,0 +1,27 @@
Channel = require "irce.modules.ngircd.channel"
describe "Channel", ->
it "requires a channel name", ->
assert.has.errors -> Channel {}
it "lets you create a channel", ->
assert.truthy Channel name: "#butts"
it "lets you set modes", ->
with Channel name: "#butts"
\add_mode "p"
assert .modes[1] == "p"
\del_mode "p"
assert #.modes == 0
it "requires channel membership to give hats", ->
with Channel name: "#butts"
assert.has.errors -> \add_hat "cadey", "@"
assert.has.errors -> \del_hat "cadey", "@"
it "lets you give people hats", ->
with Channel name: "#butts", members: {cadey: {}}
\add_hat "cadey", "@"
assert .members.cadey[1] == "o"
\del_hat "cadey", "@"
assert #.members.cadey == 0

View File

@ -0,0 +1,43 @@
modeconv = require "irce.modules.ngircd.modes"
class Channel
new: (data) =>
assert data.name
data.members = {} unless data.members
data.modes = {} unless data.modes
data.topic = "" unless data.topic
for k, v in pairs data
self[k] = v
add_hat: (who, mode) =>
hat = modeconv.convert mode
usermodes = assert @members[who\lower!]
for _, chhat in pairs usermodes
return if chhat == hat
usermodes[#usermodes+1] = hat
del_hat: (who, mode) =>
hat = modeconv.convert mode
usermodes = assert @members[who\lower!]
for i, v in pairs usermodes
if v == hat
table.remove usermodes, i
return
add_mode: (mode) =>
for _, chmd in pairs @modes
return if chmd == mode
@modes[#@modes+1] = mode
del_mode: (mode) =>
for i, v in pairs @modes
if v == mode
table.remove @modes, i
return
string.format "desync detected: mode %s isn't set on %s", mode, @name
Channel