Blob


1 ================================================================================
3 Challenge
5 In this challenge, we will modify our original dicebot.pl to convert it
6 into a bot that checks the person who messages it.
8 Every time the owner messages the channel, it will say, "You're the boss!"
9 Every time another user messages it, it will say, "I don't recognize you!"
11 ================================================================================
13 Modifying dicebot.pl
15 Let's start with our original dicebot.pl. With just a few modifications, we
16 can make the bot chat in new and interactive ways.
18 Let's edit the subroutine said:
20 sub said {
21 my $self = shift;
22 my $arguments = shift;
23 if ($arguments->{body} =~ /^!roll/) {
24 my $dice = int(rand(12))+1;
25 return "You rolled $dice!";
26 }
27 }
29 Let's change the if test. Instead of testing if the body of the message
30 starts with !roll, let's test if the sender has the right nick:
32 We replace:
34 if ($arguments->{body} =~ /^!roll/) {
36 with:
38 if ($arguments->{who} =~ /^yournick$/) {
40 Replace yournick with your nick on IRC.
42 The if (...) conditional tests whether the expression inside is true.
43 Here, we check the sender of the message with $arguments->{who}.
44 We use =~ to test if it matches the string yournick. The carat
45 symbol ^ marks the beginning of the string and the dollar symbol $
46 marks the end of the string.
48 Now that we're no longer rolling dice, we can delete this line:
50 my $dice = int(rand(12))+1;
52 Next, we should change the message. We replace:
54 return "You rolled $dice!";
56 with:
58 return "You're the boss!";
60 We now have this snippet of code:
62 if ($arguments->{who} =~ /^yournick$/) {
63 return "You're the boss!";
64 }
66 This means: If the message comes from yournick, say "You're the boss!"
67 in the same channel that the message came from.
69 Let's use an else statement to send a message for users
70 that do not match yournick:
72 else {
73 return "I don't recognize you!";
74 }
76 Finally, we will replace DiceBot on lines 5 and 20 with IDBot (because
77 this bot checks our ID):
79 package DiceBot;
81 becomes:
83 package IDBot;
85 and:
87 my $bot = DiceBot->new(
89 becomes:
91 my $bot = IDBot->new(
93 (Hint: the answer is in /home/perl102/idbot.pl)
95 ================================================================================
97 Username: perl102
98 Password: UoBnjdJd5P1
99 Server: freeirc.org
100 Port: 22
102 ================================================================================