Skip to content

Commit 20b2871

Browse files
author
Ved Gupta
committed
Initial commit
0 parents  commit 20b2871

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import discord
2+
import sys
3+
import subprocess
4+
5+
client = discord.Client()
6+
7+
@client.event
8+
async def on_ready():
9+
print(f"Logged in as {client.user}")
10+
11+
@client.event
12+
async def on_message(message):
13+
global msg
14+
msg = message
15+
if message.author == client.user : return
16+
17+
if str(message.channel) == channel and message.content.startswith("#python3"):
18+
try :
19+
run = subprocess.run(
20+
["python3", "-c", message.content], capture_output=True, text=True
21+
)
22+
await message.channel.send(f"<@{message.author.id}> \n Output : \n{run.stdout} \n\n Error : \n {run.stderr}")
23+
except Exception as e:
24+
await message.channel.send(f"Exception Occurs : {e}")
25+
26+
elif str(message.channel) == "python-interpreter" and message.content.startswith("#python2"):
27+
try :
28+
run = subprocess.run(
29+
["python2", "-c", message.content], capture_output=True, text=True
30+
)
31+
await message.channel.send(f"<@{message.author.id}> \n Output : \n{run.stdout} \n\n Error : \n {run.stderr}")
32+
except Exception as e:
33+
await message.channel.send(f"Exception Occurs : {e}")
34+
35+
if __name__ == "__main__":
36+
channel = sys.argv[2] # enter channel name you want to run bot
37+
my_secret = sys.argv[1] # enter discord bot token
38+
client.run(my_secret)

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
discord

simple-discord-bot.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import discord
3+
4+
client = discord.Client()
5+
6+
@client.event
7+
async def on_ready():
8+
""" automatic execute whwn you loged in to the system"""
9+
print("We have logged in a {0.user}".format(client))
10+
11+
@client.event
12+
async def on_message(message):
13+
"""Recieve msg to Discord and send Message"""
14+
if message.author == client.user:
15+
"""return nothing if msg is send by itself"""
16+
return
17+
# print(message.content)
18+
19+
if message.content.lower().startswith("hello"):
20+
"""Send Hello to the channel if anybody say hello"""
21+
await message.channel.send("Hello I am Bot")
22+
23+
24+
if __name__ == "__main__":
25+
my_secret = os.environ['token'] # enter your token here
26+
client.run(my_secret)

subprocess_module1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import subprocess
2+
import sys
3+
4+
# we use subprocess and sys module to run python programs
5+
python_code = """
6+
print("Program working Succesfully")
7+
"""
8+
result = subprocess.run(
9+
[sys.executable, "-c", python_code ], capture_output=True, text=True
10+
)
11+
print("stdout:", result.stdout) # command Output
12+
print("stderr:", result.stderr) # Error if occurs

subprocess_module2.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import subprocess
2+
3+
# we use subprocess module to run cmd commands
4+
5+
python_code = """
6+
print("Program working Succesfully")
7+
"""
8+
9+
# code is a list contain command each world seperated with ,
10+
code = ["python3" , "-c" , """print("working subprocess_module.py")"""]
11+
result = subprocess.run(
12+
code, capture_output=True, text=True
13+
)
14+
print("stdout:", result.stdout) # command Output
15+
print("stderr:", result.stderr) # Error if occurs

sys_module.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import sys
2+
# module use for direct input with file and exit with returncode
3+
4+
# print(sys.argv)
5+
6+
if len(sys.argv) == 1:
7+
print("Please pass the arguments")
8+
sys.exit(1) # if no argument passed so program end with returncode1
9+
10+
else:
11+
print(len(sys.argv))
12+
print(sys.argv)
13+
sys.exit(0)
14+
15+
# Run in Bash
16+
# python sys_module.py 1 2 3 4 5 6
17+
# python sys_module.py
18+
# Output : ['sys_module.py', '1', '2', '3', '4', '5', '6']
19+
20+
# check return code in bash : echo$?

0 commit comments

Comments
 (0)