-
Notifications
You must be signed in to change notification settings - Fork 7
/
bot.py
176 lines (152 loc) · 4.85 KB
/
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from discord.ext import commands
import discord
from cogs.utils import checks
import datetime
import re
import json
import asyncio
import copy
import logging
import traceback
import os
import sys
import requests
import linecache
import string
initial_extensions = [
'cogs.Funstuff',
'cogs.Gambling',
'cogs.Music',
'cogs.Searches',
'cogs.Botrelated',
'cogs.Mods'
]
botdesc = '''Luna is a simple bot made by Floretta for fun ¯\_(ツ)_/¯'''
discord_logger = logging.getLogger('discord')
discord_logger.setLevel(logging.CRITICAL)
log = logging.getLogger()
log.setLevel(logging.INFO)
handler = logging.FileHandler(filename='luna.log', encoding='utf-8', mode='w')
log.addHandler(handler)
bot = commands.Bot(command_prefix="PLACEHOLDER", description=botdesc, pm_help=None)
bot.oldsay = bot.say
async def say(content):
def SplitContent(content):
content = content.replace("@everyone", "@\u200Beveryone").replace(
"@here", "@\u200Bhere").strip()
msgs = []
while len(content) > 2000:
leeway = 2000 - (len(content) % 2000)
index = content.rfind("\n", 0, 2000)
if index < leeway:
index = content.rfind(" ", 0, 2000)
if index < leeway:
index = 2000
temp = content[0:index].strip()
if temp != "":
msgs.append(temp)
content = content[index:].strip()
if content != "":
msgs.append(content)
return msgs
for msg in SplitContent(content):
messagesent = await bot.oldsay(msg)
return messagesent
bot.say = say
async def send_cmd_help(ctx):
if ctx.invoked_subcommand:
pages = bot.formatter.format_help_for(ctx, ctx.invoked_subcommand)
for page in pages:
await bot.send_message(ctx.message.channel, page)
else:
pages = bot.formatter.format_help_for(ctx, ctx.command)
for page in pages:
await bot.send_message(ctx.message.channel, page)
@bot.event
async def on_command_error(e, ctx):
if isinstance(e, commands.MissingRequiredArgument):
await send_cmd_help(ctx)
elif isinstance(e, commands.BadArgument):
await send_cmd_help(ctx)
else:
raise(e)
@bot.event
async def on_ready():
print('\n\n\nLogged in as')
print(bot.user.name)
print(bot.user.id)
print('--------------')
print('In servers:')
print("\n".join(s.name for s in bot.servers if s.name))
print('--------------')
bot.bot_id = (await bot.application_info()).id
if sys.platform == "win32":
if os.path.exists(os.path.join(os.getcwd(), "libopus.dll")):
found = "libopus"
else:
found = False
else:
found = find_library("opus")
if found:
print(">> Loaded libopus from {}".format(found))
discord.opus.load_opus(found)
else:
if sys.platform == "win32":
print(">> Downloading libopus for Windows.")
sfbit = sys.maxsize > 2 ** 32
if sfbit:
to_dl = 'x64'
else:
to_dl = 'x86'
r = requests.get(
"https://github.com/SexualRhinoceros/MusicBot/raw/develop/libopus-0.{}.dll".format(
to_dl),
stream=True)
with open("libopus.dll", 'wb') as f:
for chunk in r.iter_content(256):
f.write(chunk)
discord.opus.load_opus("libopus")
del sfbit, to_dl
else:
print(">> Cannot load opus library - cannot use voice.")
del found
@bot.event
async def on_resumed():
print('resumed bot...')
@bot.event
async def on_command(command, ctx):
message = ctx.message
destination = None
if message.channel.is_private:
destination = 'Private Message'
else:
destination = '#{0.channel.name} ({0.server.name})'.format(message)
log.info('{0.timestamp}: {0.author.name} in {1}: {0.content}'.format(
message, destination))
def load_credentials():
with open('config.json') as f:
return json.load(f)
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj))
if __name__ == '__main__':
credentials = load_credentials()
bot.starttime = datetime.datetime.now()
bot.command_prefix = credentials['prefix']
bot.ownerid = credentials['ownerid']
for extension in initial_extensions:
try:
bot.load_extension(extension)
except:
PrintException()
bot.run(credentials['token'])
handlers = log.handlers[:]
for hdlr in handlers:
hdlr.close()
log.removeHandler(hdlr)