This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
/// <summary> | |
/// TaskCompletionSource를 이용한 비동기 반환 클래스 입니다. | |
/// JavaScript의 Promise를 모방한 클래스입니다. | |
/// </summary> | |
/// <remarks> | |
/// JavaScript에서의 Promise (MDN Docs): | |
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import request, json | |
r = requests.post("https://siro.dev/api/urls") # https://siro.dev/api/urls에 POST로 요청을 보냅니다. | |
data = r.text # 응답값을 data에 저장합니다 HTTP 상태 코드를 원할 경우, r.status_code를 사용합니다. | |
data_loaded = json.loads(data) # 응답값이 JSON이기에, Python에서 사용할 수 있도록 변환 시켜줍니다. 그리고 data_loaded에 저장합니다. | |
# 이제 원하는 데이터를 data_loaded["데이터 이름"] 으로 가져올 수 있습니다! (List의 경우 문자열(따옴표) 대신 정수를 넣어 Index를 사용할 수 있습니다) | |
# Ex : data_loaded["api"]["date"] 로 "190806" 데이터를 가져 올 수 있습니다 (API 답변 상 {"api": {"date": "190806"}, (생략)} 의 구조임) | |
# ---------------------------------------------------------------- # |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
🌞 Morning 155 commits ████▎░░░░░░░░░░░░░░░░ 20.8% | |
🌆 Daytime 253 commits ███████▏░░░░░░░░░░░░░ 34.0% | |
🌃 Evening 294 commits ████████▎░░░░░░░░░░░░ 39.5% | |
🌙 Night 43 commits █▏░░░░░░░░░░░░░░░░░░░ 5.8% |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 별도의 모듈 설치가 필요 없습니다. 아래처럼 random 모듈만 불러 와 주시면 됩니다. | |
import random | |
# 구현하는 방법은 많은데, 일단 조금 보기 쉬운 방법으로 구문을 작성하겠습니다. | |
# 보통은 if message.content == "가위": if message.content == "바위": 같이 if를 다 따로 짜지만, | |
# 이번에는 입력한 명령어가 "가위", "바위", "보" 셋 중에 하나라면 반응하도록 if문을 작성 하겠습니다. | |
if message.content == "가위" or message.content == "바위" or message.content == "보": | |
# 그리고 나서, 다른 구문을 먼저 짜기 전에, 봇이 무엇을 낼지 정하겠습니다. | |
# 가위/바위/보 총 3가지 경우의 수가 있으니, 1부터 3까지 수 중에서 아무거나 뽑아서 bot_response 라는 변수에 담아보겠습니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# await 메시지변수.add_reaction("Emoji") 로 반응을 추가합니다. | |
# 유저가 입력한 명령어에 반응을 달고 싶으시면 '메시지변수'를 message로, | |
# 전송하신 메시지에 담고 싶으시면 message.channel.send() 구문 앞에 sent = 라고 쓰시고, | |
# '메시지변수'를 sent로 적으시면 됩니다. | |
# 모든 메시지에 대한 반응 추가를 감지하지는 못하고, 특정한 메시지에 대한 반응만 감지 할 수 있습니다. | |
# 그 전에, 아래와 같은 구문을 사용하여 어떤 반응을 추가 할 때만 반응할지 정의합니다. | |
def question_check(reaction, user): # question_check 라는 이름은 마음대로 바꾸셔도 됩니다. | |
# 누구든지 :one:라는 이모지나 :two:라는 이모지를 눌렀을 때 반응하게 됩니다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 메시지 내용에서 "!명령어"를 "가나다라"로 바꾼 다음, 바꾼 내용을 message_replaced 변수에 담습니다. | |
# "가나다라"를 ""로 두시면 메시지 내용에서 "!명령어"를 지워버리는 구문이 됩니다. | |
message_replaced = message.content.replace("!명령어", "가나다라") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 우리가 보통 @멘션을 하면 <@유저ID> 의 형태로 입력됩니다. | |
# 우리는 이 점을 이용해서 유저 정보를 담은 변수까지 만들어 보겠습니다. | |
ids = re.findall(r'\d+', message.content) # 메시지에서 숫자만 다 뽑아내서 list로 저장합니다. | |
# 이 아래 구문은 명령어에 숫자가 없다는 가정 하에 작성되었습니다. 명령어에 숫자가 있을 경우, [0]을 다른 숫자로 바꿔주셔야 합니다. | |
# print(ids) 구문으로 모든 list를 보실 수 있습니다. 출력되는 내용을 참고해서 다른 숫자로 바꿔주시면 됩니다. | |
# list는 ['0번', '1번', '2번', ... ] 의 형태로 저장되는데, 첫번째 따옴표에 들어가는 내용이 0번입니다. 1번 아닙니다. | |
target = ids[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 이것도 메시지 전송 전에만 써주시면 됩니다. | |
# url= 뒤에는 아까 복사한 링크를 써주시면 됩니다. | |
# "나는 메시지를 보낸 유저의 프로필 사진을 가져오고 싶다!" 하시는 분은 url= 뒤에 따옴표 지우시고 message.author.avatar_url 넣으시면 됩니다. | |
# 썸네일 구역에 이미지 넣기 | |
embed.set_thumbnail(url="링크") | |
# 메시지 구역에 이미지 넣기 | |
embed.set_image(url="링크") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 우리가 메인제목과 설명을 작성했던 코드 아래에 써줍시다. | |
# 메시지 전송 전에만 써주면 됩니다. | |
# add_field는 여러 개 추가할 수 있으며, 추가한 순서대로 출력됩니다. | |
embed.add_field(name="소제목", value="설명", inline=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 이번 강좌를 위해서 추가적인 모듈은 불러 올 필요가 없습니다. 단순히 메시지 전송 구문을 아래과 같이 바꾸면 됩니다. | |
embed = discord.Embed(title="메인 제목", description="설명", color=0x62c1cc) # Embed의 기본 틀(색상, 메인 제목, 설명)을 잡아줍니다 | |
embed.set_footer(text="하단 설명") # 하단에 들어가는 조그마한 설명을 잡아줍니다 | |
await message.channel.send(embed=embed) # embed를 포함 한 채로 메시지를 전송합니다. | |
await message.channel.send("할 말", embed=embed) # embed와 메시지를 함께 보내고 싶으시면 이렇게 사용하시면 됩니다. |
NewerOlder