forked from RibusDesigns/pineapple
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
105 lines (85 loc) · 3.57 KB
/
bot.py
File metadata and controls
105 lines (85 loc) · 3.57 KB
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
from PluginManager import PluginManager
import discord
import traceback
import asyncio
import logging
logging.basicConfig(filename='pineapple.log', filemode='a', level=logging.INFO,
format=('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
logger = logging.getLogger('discord')
logger.setLevel(logging.NOTSET)
logging.info("Starting Pineapple")
logging.info("Starting Discord Client")
# Creates a discord client, which we will use to connect and interact with the server.
# All methods with @client.event annotations are event handlers for this client.
client = discord.Client()
logging.info("Loading plugins")
# Loads and initializes the plugin manager for the bot
pm = PluginManager("plugins", client)
pm.load_plugins()
pm.register_events()
logging.info("Plugins loaded and registered")
@client.event
async def on_ready():
"""
Event handler, fires when the bot has connected and is logged in
"""
logging.info('Logged in as ' + client.user.name + " (" + client.user.id + ")")
# Change nickname to nickname in configuration
for instance in client.servers:
await client.change_nickname(instance.me, pm.botPreferences.nickName)
# Load rank bindings
pm.botPreferences.bind_roles(instance.id)
await client.change_presence(game=discord.Game(name='Use ' + pm.botPreferences.commandPrefix + 'help for help'))
await pm.handle_loop()
@client.event
async def on_message(message):
"""
Event handler, fires when a message is received in the server.
:param message: discord.Message object containing the received message
"""
try:
if message.content.startswith(pm.botPreferences.commandPrefix) and client.user.id != message.author.id:
# Send the received message off to the Plugin Manager to handle the command
words = message.content.partition(' ')
await pm.handle_command(message, words[0][len(pm.botPreferences.commandPrefix):], words[1:])
elif message.server is not None:
await pm.handle_message(message)
except Exception as e:
await client.send_message(message.channel, "Error: " + str(e))
if pm.botPreferences.get_config_value("client", "debug") == "1":
traceback.print_exc()
@client.event
async def on_typing(channel, user, when):
"""
Event handler, fires when a user is typing in a channel
:param channel: discord.Channel object containing channel information
:param user: discord.Member object containing the user information
:param when: datetime timestamp
"""
try:
await pm.handle_typing(channel, user, when)
except Exception as e:
await client.send_message(channel, "Error: " + str(e))
if pm.botPreferences.get_config_value("client", "debug") == "1":
traceback.print_exc()
@client.event
async def on_message_delete(message):
"""
Event handler, fires when a message is deleted
:param message: discord.Message object containing the deleted message
"""
try:
if message.author.name != "PluginBot":
await pm.handle_message_delete(message)
except Exception as e:
await client.send_message(message.channel, "Error: " + str(e))
if pm.botPreferences.get_config_value("client", "debug") == "1":
traceback.print_exc()
@client.event
async def on_member_join(member):
await pm.handle_member_join(member)
@client.event
async def on_member_remove(member):
await pm.handle_member_leave(member)
# Run the client and login with the bot token (yes, this needs to be down here)
client.run(pm.botPreferences.token)