-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.tcl
83 lines (77 loc) · 2.44 KB
/
common.tcl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# common.tcl --
#
# This file implements the Tcl code for common procedures using the
# discord.tcl library. Ideally, the procedures are evaluated inside a safe
# interpreter for each server, and also saved into a database for
# retrieval later.
#
# Copyright (c) 2016, Yixin Zhang
#
# See the file "LICENSE" for information on usage and redistribution of this
# file.
proc say { msg } {
sendMessage $::channel_id $msg
}
proc display_proc { name } {
if {[llength [info proc $name]] == 0} {
return
}
set args [list]
foreach arg [info args $name] {
if {[info default $name $arg default]} {
lappend args "{$arg {$default}}"
} else {
lappend args $arg
}
}
set procStr "proc $name { [join $args] } {[info body $name]}"
say "```tcl\n$procStr```"
}
proc purge { {number {1}} } {
if {![string is integer -strict $number] || $number < 1 || $number > 100} {
return
}
deleteMessage $::channel_id [dict get $::data id]
set messageIds [list]
foreach message [getMessages $::channel_id [dict create limit $number] 1] {
lappend messageIds [dict get $message id]
}
if {$number == 1} {
deleteMessage $::channel_id [lindex $messageIds 0]
} else {
bulkDeleteMessages $::channel_id $messageIds
}
}
proc coin { {count 1} } {
if {![string is integer -strict $count] || $count <= 0} {
return
}
set heads 0
set tails 0
for {set i 0} {$i < $count} {incr i} {
if {[expr {rand() < 0.5}]} {
incr heads
} else {
incr tails
}
}
set headsPercent [expr {double($heads)/$count * 100.0}]
set tailsPercent [expr {double($tails)/$count * 100.0}]
say "$count flips\n$heads heads (${headsPercent}%)\n$tails tails (${tailsPercent}%)"
}
proc onMemberJoin { data } {
# The default channel has the same ID as the server
set guildId [dict get $data guild_id]
foreach field [list username discriminator] {
set $field [dict get $data user $field]
}
sendMessage $guildId "**$username#$discriminator** has joined the server."
}
proc onMemberLeave { data } {
# The default channel has the same ID as the server
set guildId [dict get $data guild_id]
foreach field [list username discriminator] {
set $field [dict get $data user $field]
}
sendMessage $guildId "**$username#$discriminator** has left the server."
}