Skip to content

Commit

Permalink
add intents fixes #7
Browse files Browse the repository at this point in the history
In order for the bot to read message content
it needs the privileged intent called MESSAGE_CONTENT
https://discord.com/developers/docs/topics/gateway#privileged-intents
  • Loading branch information
abe-101 committed Oct 14, 2022
1 parent 806297f commit 7f3ca05
Showing 1 changed file with 79 additions and 50 deletions.
129 changes: 79 additions & 50 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#!/usr/bin/python3
''''
"""'
Author: Rehman Ali
Python version used: Python3
NOTE: Please don't mess with code if you don't understand what you are doing.
'''
"""

import conf as config
import socks
import conf as config
import socks
import discord
from discord import errors
import requests
Expand All @@ -21,76 +21,83 @@
init(autoreset=True)

colorSchemes = {
'SUCCESS': f"{Back.GREEN}{Fore.BLACK}{Style.NORMAL}",
'FAILURE': f"{Back.RED}{Fore.WHITE}{Style.BRIGHT}",
'WARNING': f"{Back.YELLOW}{Fore.BLACK}{Style.BRIGHT}",
'RESET': f"{Style.RESET_ALL}"
"SUCCESS": f"{Back.GREEN}{Fore.BLACK}{Style.NORMAL}",
"FAILURE": f"{Back.RED}{Fore.WHITE}{Style.BRIGHT}",
"WARNING": f"{Back.YELLOW}{Fore.BLACK}{Style.BRIGHT}",
"RESET": f"{Style.RESET_ALL}",
}
colorSchemes = box(colorSchemes)

logging.basicConfig(format=f'{colorSchemes.FAILURE}[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s', level=logging.ERROR)
logging.basicConfig(
format=f"{colorSchemes.FAILURE}[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s",
level=logging.ERROR,
)

intents = discord.Intents.default()
intents.message_content = True


bot = discord.Client()
bot = discord.Client(intents=intents)
baseUrl = f"https://api.telegram.org/bot{config.TELEGRAM_BOT_TOKEN}"


def replaceMentions(mentions, msg, channel):
if channel:
for ch in mentions:
# msg = msg.replace(str(f"#{ch.id}"), '')
msg = re.sub(f"<#{ch.id}>", '', msg)
msg = re.sub(f"<{ch.id}>", '', msg)
msg = re.sub(f"<*{ch.id}>", '', msg)
msg = re.sub(f"<*{ch.id}*>", '', msg)
msg = re.sub(f"<{ch.id}*>", '', msg)
msg = re.sub(f"<#{ch.id}>", "", msg)
msg = re.sub(f"<{ch.id}>", "", msg)
msg = re.sub(f"<*{ch.id}>", "", msg)
msg = re.sub(f"<*{ch.id}*>", "", msg)
msg = re.sub(f"<{ch.id}*>", "", msg)
elif not channel:
for member in mentions:
msg = re.sub(f"<@{member.id}>", '', msg)
msg = re.sub(f"<@!{member.id}>", '', msg)
msg = re.sub(f"<{member.id}>", '', msg)
msg = re.sub(f"<*{member.id}*>", '', msg)
msg = re.sub(f"<{member.id}*>", '', msg)
msg = re.sub(f"<*{member.id}>", '', msg)
msg = re.sub(f"<@{member.id}>", "", msg)
msg = re.sub(f"<@!{member.id}>", "", msg)
msg = re.sub(f"<{member.id}>", "", msg)
msg = re.sub(f"<*{member.id}*>", "", msg)
msg = re.sub(f"<{member.id}*>", "", msg)
msg = re.sub(f"<*{member.id}>", "", msg)
return str(msg)


def removeTags(msg):
msg = re.sub(r"@\w*", '', msg)
msg = re.sub(r"@\w*", "", msg)
msg = requests.utils.quote(msg)
#print(f"{colorSchemes.SUCCESS}Quoted message: {msg}")
# print(f"{colorSchemes.SUCCESS}Quoted message: {msg}")
return msg




def isPhoto(url):
imgExts = ["png", "jpg", "jpeg", "webp"]
if any(ext in url for ext in imgExts):
return True
else:
return False


def isVideo(url):
vidExts = ["mp4", "MP4", "mkv"]
if any(ext in url for ext in vidExts):
return True
else:
return False


def isDoc(url):
docExts = ["zip", "pdf", "gif"]
if any(ext in url for ext in docExts):
return True
else:
return False


def matchChannel(channel, list):
found=False
found = False
for ch in list:
res = ch.find(channel)
if str(res) != "-1":
found=True
found = True
return found


Expand All @@ -108,32 +115,46 @@ def sendMsg(url):
raise OSError
except OSError:
attempts += 1
print(f"{colorSchemes.FAILURE}[-] Sending failed!\n[+] Trying again ... (Attempt {attempts})")
print(
f"{colorSchemes.FAILURE}[-] Sending failed!\n[+] Trying again ... (Attempt {attempts})"
)
continue
except KeyboardInterrupt:
print("\n[+] Please wait untill all messages in queue are sent!\n")
else:
print(f"{colorSchemes.FAILURE}[-] Message was not sent in 5 attempts. \n[-] Please check your network.")
print(
f"{colorSchemes.FAILURE}[-] Message was not sent in 5 attempts. \n[-] Please check your network."
)
break



if config.PROXY:
if config.AUTHENTICATION:
if config.USERNAME != None and config.PASSWORD != None:
socks.set_default_proxy(socks.SOCKS5, config.SOCKS5_SERVER, config.SOCKS5_PORT, username=config.USERNAME, password=config.PASSWORD)
print(f"\n[+] Proxy enabled with authentication set!\n[+] Proxy Server: {config.SOCKS5_SERVER}:{config.SOCKS5_PORT}")
socks.set_default_proxy(
socks.SOCKS5,
config.SOCKS5_SERVER,
config.SOCKS5_PORT,
username=config.USERNAME,
password=config.PASSWORD,
)
print(
f"\n[+] Proxy enabled with authentication set!\n[+] Proxy Server: {config.SOCKS5_SERVER}:{config.SOCKS5_PORT}"
)
else:
print(f"{colorSchemes.FAILURE}[-] Proxy authentication enabled but username/password not set.")
print(
f"{colorSchemes.FAILURE}[-] Proxy authentication enabled but username/password not set."
)
quit()
elif not config.AUTHENTICATION:
socks.set_default_proxy(socks.SOCKS5, config.SOCKS5_SERVER, config.SOCKS5_PORT)
print(f"{colorSchemes.WARNING}[+] Proxy enabled without authentication!\n[+] Proxy Server: {config.SOCKS5_SERVER}:{config.SOCKS5_PORT}")
print(
f"{colorSchemes.WARNING}[+] Proxy enabled without authentication!\n[+] Proxy Server: {config.SOCKS5_SERVER}:{config.SOCKS5_PORT}"
)
socket.socket = socks.socksocket
print(f"{colorSchemes.WARNING}[+] Please wait for at least 30 seconds before first message.")



print(
f"{colorSchemes.WARNING}[+] Please wait for at least 30 seconds before first message."
)


@bot.event
Expand All @@ -144,18 +165,24 @@ async def on_message(message):
channelName = message.channel.name
except AttributeError:
pass
#print(f"Server: {serverName}, Channel: {channelName}")
# print(f"Server: {serverName}, Channel: {channelName}")
if serverName in serversList:
channelsList = config.serversList[serverName]
if matchChannel(channelName, channelsList):
print(f"\n-------------------------------------------\n[+] Channel: {channelName}")
print(
f"\n-------------------------------------------\n[+] Channel: {channelName}"
)
if message.content:
if message.mentions:
# print(f"\n----------------\nUser Mentioned\n----------------")
message.content = replaceMentions(message.mentions, message.content, channel=False)
message.content = replaceMentions(
message.mentions, message.content, channel=False
)
if message.channel_mentions:
# print(f"\n----------------\nChannel Mentioned\n----------------")
message.content = replaceMentions(message.channel_mentions, message.content, channel=True)
message.content = replaceMentions(
message.channel_mentions, message.content, channel=True
)
toSend = f"{message.guild}/{message.channel}/{message.author.name}: {message.content}"
print(f"[+] Message: {toSend}")
toSend = removeTags(toSend)
Expand All @@ -173,31 +200,31 @@ async def on_message(message):
elif isDoc(attachmentUrl):
url = f"{baseUrl}/sendDocument?document={attachmentUrl}&chat_id={config.TELEGRAM_RECEIVER_CHAT_ID}"
sendMsg(url)

if message.embeds:
embed = message.embeds[0].to_dict()
print(embed)
if str(embed['type']) == "rich":
if 'title' in embed.keys() and 'description' in embed.keys():
if str(embed["type"]) == "rich":
if "title" in embed.keys() and "description" in embed.keys():
toSend = f"{message.guild}/{message.channel}/{message.author.name}: {embed['title']}\n{embed['description']}"
toSend = removeTags(toSend)
elif 'title' in embed.keys():
elif "title" in embed.keys():
toSend = f"{message.guild}/{message.channel}/{message.author.name}: {embed['title']}"
toSend = removeTags(toSend)
elif 'description' in embed.keys():
elif "description" in embed.keys():
toSend = f"{message.guild}/{message.channel}/{message.author.name}: {embed['description']}"
toSend = removeTags(toSend)
url = f"{baseUrl}/sendMessage?text={toSend}&chat_id={config.TELEGRAM_RECEIVER_CHAT_ID}"
sendMsg(url)
# print(embed)
elif str(embed['type']) == "link":
elif str(embed["type"]) == "link":
toSend = f"{embed['title']}\n{embed['description']}\n{embed['url']}"
toSend = removeTags(toSend)
url = f"{baseUrl}/sendMessage?text={toSend}&chat_id={config.TELEGRAM_RECEIVER_CHAT_ID}"
sendMsg(url)


#Run the bot using the user token
# Run the bot using the user token
try:
bot.run(config.USER_DISCORD_TOKEN, bot=False)
except RuntimeError:
Expand All @@ -207,7 +234,9 @@ async def on_message(message):
print(f"{colorSchemes.FAILURE}Invalid discord token or network down!")
quit()
except errors.LoginFailure:
print(f"{colorSchemes.FAILURE}Login failed to discord. May be bad token or network down!")
print(
f"{colorSchemes.FAILURE}Login failed to discord. May be bad token or network down!"
)
quit()
except clientExcps.ClientConnectionError:
print(f"{colorSchemes.FAILURE}[-] Proxy seems to be down or network problem.")
Expand Down

0 comments on commit 7f3ca05

Please sign in to comment.