Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Matrix Ruby SDK
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Alexander Olofsson
Matrix Ruby SDK
Commits
e3d24879
Verified
Commit
e3d24879
authored
3 years ago
by
Alexander Olofsson
Browse files
Options
Downloads
Patches
Plain Diff
bot: Slight examples/defaults improvement
parent
9b2afb66
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/bot_api.rb
+42
-37
42 additions, 37 deletions
examples/bot_api.rb
lib/matrix_sdk/bot/base.rb
+1
-1
1 addition, 1 deletion
lib/matrix_sdk/bot/base.rb
with
43 additions
and
38 deletions
examples/bot_api.rb
+
42
−
37
View file @
e3d24879
...
...
@@ -8,6 +8,9 @@
require
'matrix_sdk/bot'
# Util methods added at end of class, to keep command listing near the top
module
Utils
;
end
set
:bot_name
,
'pingbot'
command
:spam
,
only: :dm
,
desc:
'Spams a bunch of nonsense'
do
|
message_count
=
5
|
...
...
@@ -28,42 +31,6 @@ command :echo, desc: 'Echoes the given message back as an m.notice' do |message|
room
.
send_notice
(
message
)
end
MS_PER_DAY
=
86_400_000.0
MS_PER_HOUR
=
3_600_000.0
MS_PER_MINUTE
=
60_000.0
MS_PER_SECOND
=
1_000.0
def
duration_format
(
duration_ms
)
return
"
#{
duration_ms
}
ms"
if
duration_ms
<=
9000
timestr
=
[]
if
duration_ms
>
MS_PER_DAY
*
1.1
duration_ms
-=
(
days
=
(
duration_ms
/
MS_PER_DAY
).
floor
)
*
MS_PER_DAY
timestr
<<
"
#{
days
}
days
#{
days
>
1
?
's'
:
''
}
"
if
days
.
positive?
end
if
duration_ms
>
MS_PER_HOUR
*
1.1
duration_ms
-=
(
hours
=
(
duration_ms
/
MS_PER_HOUR
).
floor
)
*
MS_PER_HOUR
timestr
<<
"
#{
hours
}
hour
#{
hours
>
1
?
's'
:
''
}
"
if
hours
.
positive?
end
if
duration_ms
>
MS_PER_MINUTE
*
1.1
duration_ms
-=
(
minutes
=
(
duration_ms
/
MS_PER_MINUTE
).
floor
)
*
MS_PER_MINUTE
timestr
<<
"
#{
minutes
}
minute
#{
minutes
>
1
?
's'
:
''
}
"
if
minutes
.
positive?
end
seconds
=
(
duration_ms
/
MS_PER_SECOND
).
round
(
timestr
.
empty?
?
1
:
0
)
seconds
=
seconds
.
round
if
seconds
.
round
==
seconds
timestr
<<
"
#{
seconds
}
second
#{
seconds
>
1
?
's'
:
''
}
"
if
seconds
.
positive?
if
timestr
.
count
>
2
last
=
timestr
.
pop
[
timestr
.
join
(
', '
),
last
].
join
(
' and '
)
else
timestr
.
join
' and '
end
end
command
:ping
,
desc:
'Runs a ping with a given ID and returns the request time'
do
|
message
=
nil
|
origin_ts
=
Time
.
at
(
event
[
:origin_server_ts
]
/
1000.0
)
diff
=
Time
.
now
-
origin_ts
...
...
@@ -80,7 +47,7 @@ command :ping, desc: 'Runs a ping with a given ID and returns the request time'
sender:
sender
.
id
,
room:
room
.
id
,
event:
event
.
event_id
,
time:
duration_format
(
milliseconds
),
time:
Utils
.
duration_format
(
milliseconds
),
msg:
message
}
...
...
@@ -104,3 +71,41 @@ command :ping, desc: 'Runs a ping with a given ID and returns the request time'
room
.
send_custom_message
(
format
(
plaintext
,
formatdata
),
eventdata
,
msgtype:
'm.notice'
)
end
module
Utils
MS_PER_DAY
=
86_400_000.0
MS_PER_HOUR
=
3_600_000.0
MS_PER_MINUTE
=
60_000.0
MS_PER_SECOND
=
1_000.0
def
self
.
duration_format
(
duration_ms
)
return
"
#{
duration_ms
}
ms"
if
duration_ms
<=
9000
timestr
=
[]
if
duration_ms
>
MS_PER_DAY
*
1.1
duration_ms
-=
(
days
=
(
duration_ms
/
MS_PER_DAY
).
floor
)
*
MS_PER_DAY
timestr
<<
"
#{
days
}
days
#{
days
>
1
?
's'
:
''
}
"
if
days
.
positive?
end
if
duration_ms
>
MS_PER_HOUR
*
1.1
duration_ms
-=
(
hours
=
(
duration_ms
/
MS_PER_HOUR
).
floor
)
*
MS_PER_HOUR
timestr
<<
"
#{
hours
}
hour
#{
hours
>
1
?
's'
:
''
}
"
if
hours
.
positive?
end
if
duration_ms
>
MS_PER_MINUTE
*
1.1
duration_ms
-=
(
minutes
=
(
duration_ms
/
MS_PER_MINUTE
).
floor
)
*
MS_PER_MINUTE
timestr
<<
"
#{
minutes
}
minute
#{
minutes
>
1
?
's'
:
''
}
"
if
minutes
.
positive?
end
seconds
=
(
duration_ms
/
MS_PER_SECOND
).
round
(
timestr
.
empty?
?
1
:
0
)
seconds
=
seconds
.
round
if
seconds
.
round
==
seconds
timestr
<<
"
#{
seconds
}
second
#{
seconds
>
1
?
's'
:
''
}
"
if
seconds
.
positive?
if
timestr
.
count
>
2
last
=
timestr
.
pop
[
timestr
.
join
(
', '
),
last
].
join
(
' and '
)
else
timestr
.
join
' and '
end
end
end
This diff is collapsed.
Click to expand it.
lib/matrix_sdk/bot/base.rb
+
1
−
1
View file @
e3d24879
...
...
@@ -571,7 +571,7 @@ module MatrixSdk::Bot
:help
,
desc:
'Shows this help text'
,
notes:
<<~
NOTES
For commands that
includ
e multiple
separate
arguments, you will need to use quotes
where they contain
spaces
For commands that
tak
e multiple arguments, you will need to use quotes
around
spaces
E.g. !login "my username" "this is not a real password"
NOTES
)
do
|
command
=
nil
|
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment