Skip to content

Instantly share code, notes, and snippets.

@ShapeLayer
Created October 4, 2020 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ShapeLayer/35d8b190d9af386739be43a7157a52d1 to your computer and use it in GitHub Desktop.
Save ShapeLayer/35d8b190d9af386739be43a7157a52d1 to your computer and use it in GitHub Desktop.
Discord Picture Attachmented Message Thrower
# thrower.py
# Need to Install discord.py Module
# Need to Create 'token' File
import discord
import sqlite3
import re
client = discord.Client()
token = open('token', encoding='utf-8').read()
conn = sqlite3.connect('db.db', check_same_thread = False)
curs = conn.cursor()
rerule = [
r'\<\#([0-9].*?)\>', # Channel
r'\<\@\!([0-9].*?)\>' # User
]
pic_ext = ['jpg', 'jpeg', 'bmp', 'gif', 'png', 'webp']
curs.executescript('''
CREATE TABLE IF NOT EXISTS blacklist (
server INTEGER NOT NULL,
user INTEGER NOT NULL
);
''')
curs.executescript('''
CREATE TABLE IF NOT EXISTS watch (
server INTEGER NOT NULL,
watch INTEGER NOT NULL,
throwto INTEGER NOT NULL
);
''')
conn.commit()
HELPMSG = '''
======= Thrower =======
`던지기 도움!`
:이 봇의 명령어를 확인합니다.
`던지기 채널! 추가 #감시할채널 #던질채널`
:`#던질채널`로 `#감시할채널`의 메시지를 던지도록 설정합니다.
`던지기 사람! 추가 @사용자`
:멘션한 서버의 사용자를 봇이 감시합니다.
`던지기 채널! 삭제 #감시하던채널 #던지던채널`
:`@던지던채널`로 `#감시하던채널`의 메시지를 더 이상 던지지 않도록 설정합니다.
`던지기 사람! 삭제 @사용자`
:멘션한 서버의 사용자를 봇이 더 이상 감시하지 않습니다.
'''
def parsing(exe, txt):
if exe == 0: # exe == 0 => Channel
r = rerule[0]
else: # exe == 1 => User
r = rerule[1]
compiled = re.compile(r)
matchobj = compiled.search(txt)
return matchobj.group(1)
def ctl_channel(exe, server, watch, throwto):
if exe == 0: # exe == 0 => 추가
curs.execute('INSERT INTO watch (server, watch, throwto) VALUES(?, ?, ?)', [server, watch, throwto])
conn.commit()
else: # exe == 1 => 삭제
curs.execute('DELETE FROM watch WHERE server = ? and watch = ? and throwto = ?', [server, watch, throwto])
conn.commit()
def ctl_blacklist(exe, server, user):
if exe == 0: # exe == 0 => 추가
curs.execute('INSERT INTO blacklist (server, user) VALUES(?, ?)', [server, user])
conn.commit()
else: # exe == 1 => 삭제
curs.execute('DELETE FROM blacklist WHERE server = ? and user = ?', [server, user])
conn.commit()
@client.event
async def on_ready():
print('봇 시작됨.')
print('봇 이름: ' + client.user.name)
print('봇 아이디: ' + str(client.user.id))
print('------')
activity = discord.Activity(name = '"던지기 도움!" 을 입력해 도움말 확인', type = discord.ActivityType.watching)
await client.change_presence(activity = activity)
@client.event
async def on_message(message):
con = message.content
if con.startswith('던지기'): # 명령어 셋
con_split = con.split()
if len(con_split) > 1:
if con_split[1] == '도움!':
await message.channel.send(HELPMSG)
elif len(con_split) > 2 and message.author.permissions_in(message.channel).administrator:
if con_split[1] == '채널!':
if con_split[2] == '추가':
ctl_channel(0, message.guild.id, parsing(0, con_split[3]), parsing(0, con_split[4]))
await message.channel.send('완료')
elif con_split[2] == '삭제':
ctl_channel(1, message.guild.id, parsing(0, con_split[3]), parsing(0, con_split[4]))
await message.channel.send('완료')
elif con_split[1] == '사람!':
if con_split[2] == '추가':
ctl_blacklist(0, message.guild.id, parsing(1, con_split[3]))
await message.channel.send('완료')
elif con_split[2] == '삭제':
ctl_blacklist(1, message.guild.id, parsing(1, con_split[3]))
await message.channel.send('완료')
elif len(con_split) > 2 and not message.author.permissions_in(message.channel).administrator:
await message.channel.send('당신은 날 마음대로 조종할 수 없어요.')
curs.execute('SELECT throwto FROM watch WHERE watch = ?', [message.channel.id])
throws = curs.fetchall()
if throws and message.author.id != client.user.id:
for throw in throws:
throw = throw[0]
curs.execute('SELECT user FROM blacklist WHERE server = ?', [message.guild.id])
blacklist = curs.fetchall()
for user in blacklist:
if user[0] == message.author.id:
if message.attachments:
for ext in pic_ext:
if message.attachments[0].filename.endswith(ext):
await message.channel.send('<@!{}>가 보낸 첨부파일은 <#{}>로 던집니다.'.format(message.author.id, throw))
await client.get_channel(throw).send('<@!{}>가 보냈으나 던져짐\n{}'.format(message.author.id, message.attachments[0].url))
await message.delete()
if __name__ == '__main__':
client.run(token)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment