Created
February 25, 2018 10:33
-
-
Save vampjaz/0b7791d554afaee34eb1e23ce425e281 to your computer and use it in GitHub Desktop.
example usage of a SydDiscord + Dogecord bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from SydDiscord import Discord | |
from dogecord import BotFramework | |
## here would probably be where we load the secret keys, configuration, and such | |
dc = Discord(authentication thingies) | |
bot = BotFramework(dc, config_stuff) # the config would point to a module folder | |
bot.load_modules() | |
if __name__ == '__main__': | |
bot.run_event_loop() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
# when the main file calls load_modules, the `bot' class is injected into the dogecord namespace so it can be referenced here. | |
# context is dynamically set inside the wrapper before each handler function is run (like flask a bit) | |
# by default, the send_msg, request_msg, etc will also be bound by context. however, they can be parameterized to go somewhere else (dm for example) | |
from dogecord import bot, context, send_msg, request_msg, render_help, EVENT_JOIN | |
message_counter = 0 | |
@bot.subscribe_msg(in_server=True, not_self=True) # these flags tell the event loop to only pass in messages that are in a channel on a server and not from the bot | |
def on_message(): | |
global message_counter | |
message_counter += 1 | |
@bot.subscribe_cmd('messages', help='shows the number of messages since the bot was started', aliases=['messagecount']) | |
def message_count(): | |
send_msg('messages seen: {}'.format(message_counter)) | |
@bot.subscribe_cmd('help', help='lists the commands') | |
def command_list(): | |
send_msg(render_help(), split=True) # this generates a command list from the help messages provided, and splitting it into multiple messages if nessecary | |
@bot.subscribe_event(EVENT_JOIN, in_server=True) | |
def announce_join(): | |
nick = context.user.nickname # context.user would be the user that sent the event (if it's a user-generated event) | |
send_msg('welcome {} to the server!'.format(nick)) | |
@bot.subscribe_cmd('choice', help='pick between different items') | |
def choice_cmd(): | |
chosen = random.choice(context.cmd.args) # context.cmd contains info about the command executed. context.cmd.args is a list of the words that came after the command invocation | |
send_msg('i choose {}'.format(chosen)) | |
@bot.subscribe_cmd('guess', help='guess the number the bot is thinking of!') | |
def guess_cmd(): | |
upper = random.randint(10, 100) | |
lower = random.randint(0,upper-5) | |
number = random.randint(lower,upper) | |
send_msg('i\'m thinking of a number between {} and {}'.format(lower,upper)) | |
for i in range(5): | |
guessed = request_msg('guess my number!', timeout=10) # this acts like python's input() but only responds to the user in the request context by default; it also goes back to the event loop asynchronulsy while it waits for the message or until 10 seconds pass (which is of course adjustable) | |
if not guessed: | |
send_msg('you took too long!') | |
break | |
else: | |
try: | |
guessed = int(guessed) | |
except ValueError: | |
send_msg('that\'s not a number') | |
if guessed == number: | |
send_msg('you got it right!') | |
return | |
if guessed > number: | |
send_msg('too high') | |
if guessed < number: | |
send_msg('too low') | |
send_msg('you\'re out of tries') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment