-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
95 lines (85 loc) · 3.58 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
import shutil
import discord
import requests
import responses
from discord.utils import get
with open('logFiles/Channels.log', 'r', encoding='cp1252') as fh:
channels = fh.read().split("\n")[0].replace(' ', '').split(':')[1].replace('>>>', ' ').split(',')
reactionModeIsOn = False
reactionUser = ''
async def send_message(message, is_private, toSendBack):
if str(toSendBack)[0:9] == 'SEND FILE':
filePath = toSendBack[11:len(toSendBack)]
await message.channel.send(file=discord.File(filePath))
else:
try:
if toSendBack is not None and toSendBack is not Exception:
await message.author.send(toSendBack) if is_private else await message.channel.send(toSendBack)
except Exception as e:
print(e)
def saveImage(message, imgName):
try:
url = message.attachments[0].url
except IndexError:
url = None
else:
if url[0:26] == "https://cdn.discordapp.com": # look to see if url is from discord
r = requests.get(url, stream=True)
imageName = 'Reactions/' + imgName + url.split('.')[
len(url.split('.')) - 1] # uuid creates random unique id to use for image names
with open(imageName, 'wb') as out_file:
print('Saving image: ' + imageName)
shutil.copyfileobj(r.raw, out_file)
return 'Reactions/' + imgName + url.split('.')[len(url.split('.')) - 1]
return None
rpsModeOn = False
rpsPlayer = ''
joinRoleName = 'Test'
def run_discord_bot():
# Bot settings
TOKEN = input("ENTER KEY << ")
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
# Boot message
@client.event
async def on_ready():
print(f'{client.user} is now running!')
# Message Event Handler
@client.event
async def on_message(message):
try:
with open('logFiles/Channels.log', 'r', encoding='cp1252') as fh:
raw = fh.read()
if message.author == client.user:
return
# Data about message
username = str(message.author)
userID = str(message.author.id)
guild = str(message.guild.name)
user_message = str(message.content)
channel = str(message.channel.name)
isBot = bool(message.author.bot)
if user_message[0] == '?': # Is this a private message?
user_message = user_message[1:]
response = responses.handle_response(message=user_message, username=username, guild=guild,
userID=userID, isBot=isBot, messageHandle=message)
await send_message(message=message, is_private=True, toSendBack=response)
else:
# Am I allowed to use this channel?
isGoodChannel = False
for chan in channels:
if channel == chan:
isGoodChannel = True
if isGoodChannel:
response = responses.handle_response(message=user_message, username=username, guild=guild,
userID=userID, isBot=isBot, messageHandle=message)
await send_message(message=message, is_private=False, toSendBack=response)
except IndexError:
pass
@client.event
async def on_member_join(member):
print(member, type(member))
role = get(member.guild.roles, name=joinRoleName)
await member.add_roles(role)
client.run(TOKEN)