-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathHuhnbot.py
executable file
·108 lines (88 loc) · 3.6 KB
/
Huhnbot.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
import discord
import os
import subprocess
import sys
from neuralintents import GenericAssistant
MODEL_NAME = 'huhn'
# Setup Chat bot
chatbot = GenericAssistant('intents.json', model_name=MODEL_NAME)
chatbot.train_model()
chatbot.save_model()
# Load Discord API Token from file
def loadToken():
TOKEN_PATH = 'token.txt'
token = ''
if os.path.exists(TOKEN_PATH):
with open(TOKEN_PATH, 'r') as file:
token = file.read().replace('\n', '')
# (if file doesn't exist, ask for it via standard input, and save it)
else:
token = input("Discord Token:")
with open(TOKEN_PATH, 'w') as file:
file.write(token)
return token
# This bot requires the members and reactions intents.
#intents = discord.Intents.default()
#intents.members = True
client = discord.Client()#intents=intents)
# On Startup
@client.event
async def on_ready():
message = "[Started] Logged in as {0.user}".format(client)
print(message)
if len(sys.argv) == 1 or sys.argv[1] != '-debug':
channel = client.get_channel(817402120063549451)
await channel.send(message)
# Helper functions
def fromMod(message):
is_admin = message.author.top_role.permissions.administrator
#is_admin = discord.utils.find(lambda r: r.name == "Admin", message.guild.roles) in message.author.roles
is_mod = discord.utils.find(lambda r: r.name == "Moderator", message.guild.roles) in message.author.roles
return is_admin or is_mod
# On Message
@client.event
async def on_message(message):
if message.author == client.user:
return
# [===== Message to Huhnbot =====] #
if "huhnbot" in message.content.lower() or client.user.mentioned_in(message):
send_as_reply = False
message_content = message.content.lower().replace("huhnbot","")
# When pinged only with "huhnbot", reply to earlier message and delete ping
if message.reference is not None and message_content.lower() == "huhnbot" and fromMod(message):
pinged_message = message
ref_message = await message.channel.fetch_message(message.reference.message_id)
message = ref_message
send_as_reply = True
message_content = ref_message.content.lower().replace("huhnbot","")
await pinged_message.delete()
response = chatbot.request(message_content)
if send_as_reply:
await message.reply(response)
else:
await message.channel.send(response)
return
# [===== Debug =====] #
if message.content.startswith("huhndebug "):
if not fromMod(message):
await message.channel.send(f"Hahahah. Nope!")
return
cmd = message.content[10:]
# [UPDATE]
if cmd == 'update':
try:
process = subprocess.Popen(["git", "pull"], stdout=subprocess.PIPE)
output = process.communicate()[0].decode("utf-8")
await message.channel.send(f"Huhnbot updated by {message.author} ```{output}```")
except Exception as e:
await message.channel.send(f"git pull not working :frowning: ```{e}```")
print("git pull not working :(")
# [RESTART]
elif cmd == 'restart':
await message.channel.send(f"Ok boss. Restarting now....")
os.execv(sys.executable, ['python3'] + sys.argv)
# [404]
else:
await message.channel.send(f"Don't know '{cmd}'")
# Start Bot
client.run(loadToken())