/
discord-math-bot.py
69 lines (50 loc) 路 1.91 KB
/
discord-math-bot.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import discord
import traceback
import asyncio
import os
import sys
from discord.ext import commands
BOT_PREFIX = ("?", "!", ">", "*", "-", "_", ":", ".", "^", "<", "+", "&")
TOKEN="YOUR_DISCORD_BOT_TOKEN_HERE"
TOKEN=os.environ.get("DISCORD_BOT_SECRET")
PREFIX="!"
author_ids=[679306420587462667]
bot = commands.Bot(command_prefix=PREFIX)
@bot.event
async def on_message(message):
# ------------- IMPORTANT ----------------
# if you want to restrict access uncomment the next two line
if message.author.id not in author_ids:
print(message.author.id)
return
if message.author.bot:
return
await bot.process_commands(message)
@bot.event
async def on_ready():
print("Math Bot is Online.")
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.NoPrivateMessage):
await ctx.author.send('This command cannot be used in private messages.')
elif isinstance(error, commands.DisabledCommand):
await ctx.author.send('Sorry. This command is disabled and cannot be used.')
elif isinstance(error, commands.CommandInvokeError):
original = error.original
if not isinstance(original, discord.HTTPException):
print(f'In {ctx.command.qualified_name}:', file=sys.stderr)
traceback.print_tb(original.__traceback__)
print(f'{original.__class__.__name__}: {original}', file=sys.stderr)
elif isinstance(error, commands.ArgumentParsingError):
await ctx.send(error)
@bot.group(name="calc")
async def _u(ctx):
if not ctx.invoked_subcommand:
return await ctx.send("Use a sub command like 'add', 'eval'")
@_u.command(name="add")
async def _uadd(ctx,first_num,second_num):
return await ctx.send(f"{first_num}+{second_num}={int(first_num)+int(second_num)}")
@_u.command(name="eval")
async def _ueval(ctx,expresssion):
return await ctx.send(f"{eval(expresssion)}")
bot.run(TOKEN)