-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprinterbot.py
49 lines (41 loc) · 1.52 KB
/
printerbot.py
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
import discord
import os
from discord.ext import commands
# Discord related variables
bot = commands.Bot(command_prefix="$")
token_file = open("token.txt", "r")
token = token_file.readline()
# Bot variables
owner = "Akselmo"
msg_rate = 3
cooldown_seconds = 3
printer_command = "/tmp/DEVTERM_PRINTER_IN"
#printer_command = "test.txt"
illegal_chars = set(r'&|;$><`\\')
log = print #rename print to log since print is used by bot commands
# Test command
@bot.command()
async def ping(ctx):
await ctx.send("Pong!")
# Send message to devterm
@bot.command()
@commands.cooldown(msg_rate, cooldown_seconds, commands.BucketType.guild) #whole guild is in cooldown
async def print(ctx, *, arg):
arg = str(arg)
await ctx.send('Okay, printing "{}" from {}\'s Devterm printer...'.format(arg, owner))
log('{0.author} is printing message: {1}'.format(ctx, arg))
if any((c in illegal_chars) for c in arg):
await ctx.send("Illegal chars in message!")
log('{0.author} used illegal characters! Naughty!'.format(ctx))
else:
user_line = '{0.author} sent: \n'.format(ctx)
message = '{} \n \n \n \n \n \n \n \n \n'.format(arg) #more newlines so its easier to see the message
cmd = 'echo "{}{}" > {}'.format(user_line, message, printer_command)
os.system(cmd)
#Prints error during cooldown
@print.error
async def print_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
log("Bot is in cooldown!")
await ctx.send("Hold on let me cool down!")
bot.run(str(token))